長時間讀者首次提交。我對R相對比較陌生,而我正處在一個項目的中間,試圖完成今天到期的重要部分:)。通過R data.table迭代嘗試調用函數的問題
我想遍歷data.table並將函數應用於data.table的每一行。對我來說奇怪的是,我可以單獨傳遞每個值,它可以工作,但是當我嘗試迭代lapply等。人。或for循環我無法讓該函數工作。我試圖儘可能多地包含代碼等信息。我對這個長度表示歉意。
data.table包含捐助者信息。我遮住了donor_id。
> str(testdonors)
Classes ‘data.table’ and 'data.frame': 22 obs. of 34 variables:
$ donor_id : Factor w/ 455 levels "XXYYWWW","WWWXXYY",..: 281 282 283 284 285 286 289 290 291 292 ...
$ Date_of_Death : POSIXct, format: "2012-01-03" "2012-01-03" "2012-01-02" "2012-01-02" ...
$ rec_out_us : int 0 0 0 0 0 0 0 0 0 0 ...
$ age : num 20.1 22.9 54.3 28.7 43.4 ...
$ gender : Factor w/ 2 levels "Female","Male": 2 2 1 2 1 2 2 2 1 2 ...
$ race : Factor w/ 4 levels "Black","Hispanic",..: 4 4 4 2 4 2 4 3 4 2 ...
$ cause_death : Factor w/ 4 levels "Anoxia","Other Cause",..: 1 4 3 3 1 3 3 4 3 4 ...
$ circum_death : Factor w/ 5 levels "Homicide","Natural",..: 4 5 2 2 3 2 2 1 2 5 ...
$ method_death : Factor w/ 7 levels "Asphyxiation",..: 1 5 7 7 6 7 7 4 7 5 ...
$ blood_type : Factor w/ 4 levels "A","AB","B","O": 4 4 1 4 1 4 4 2 1 4 ...
$ bmi : num 20.4 19.6 24.9 27.4 25.7 ...
$ serum_creat : num 0.69 1.8 0.64 0.81 0.7 1.31 2.67 0.6 0.57 0.95 ...
$ hb_core_anti : int 0 0 1 0 0 0 0 0 0 0 ...
$ hb_surface_anti : int 0 0 0 0 0 0 0 0 0 0 ...
$ hc_anti : int 0 0 0 0 0 0 0 0 0 0 ...
$ cis_blood : int 0 0 0 0 0 0 0 0 0 0 ...
$ cis_lung : int 0 0 0 0 0 0 0 0 0 0 ...
$ cis_urine : int 0 0 0 0 0 0 0 0 0 0 ...
$ cis_other : int 0 0 0 0 0 0 0 0 0 0 ...
$ dcd_non_hb : int 0 1 0 1 1 0 0 0 0 0 ...
$ dcd_controlled : int 0 1 0 1 1 0 0 0 0 0 ...
$ ca_after_bd : int 0 0 0 0 0 0 0 0 0 0 ...
$ lung_p02 : num 117 63 143 104 379 ...
$ cigarette_use : int 0 0 0 0 0 0 0 0 0 0 ...
$ cigarette_use_6mos: int 0 0 0 0 0 0 0 0 0 0 ...
$ cocaine_use : int 0 0 0 0 1 0 1 0 0 0 ...
$ cocaine_use_6mos : int 0 0 0 0 0 0 1 0 0 0 ...
$ drug_use_other : int 1 1 1 0 1 0 1 0 0 0 ...
$ heavy_alcohol : int 0 0 0 0 1 0 0 0 0 0 ...
$ high_risk_donor : int 1 0 1 0 0 0 0 0 0 0 ...
$ history_diabetes : int 0 0 0 0 0 0 0 0 0 0 ...
$ insulin_dep : int 0 0 0 0 0 0 0 0 0 0 ...
$ history_hypten : int 0 0 1 0 0 0 0 0 0 0 ...
$ history_cancer : int 0 0 0 0 0 0 0 0 0 0 ...
- attr(*, "sorted")= chr "donor_id"
- attr(*, ".internal.selfref")=<externalptr>
功能1
calc_donor_yield <- function(p_model_id, p_donor){
heart_yield <- calc_heart_yield(p_model_id, p_donor)
...
}
功能2
calc_heart_yield <- function(p_model_id, p_donor){
if(p_donor[, hb_core_anti == 1 || hb_surface_anti == 1 || hc_anti == 1]){
heart_yield = 0.0036
} else {
heart_yield <- get_organ_yield(p_model_id,"heart_coeff",p_donor)
}
heart_yield
}
這工作! test1的< - calc_donor_yield(1 testdonors [2])
這些不...
lapply(testdonors["donor_id",], function(x) calc_donor_yield(1,x))
Error in NextMethod("[") : object 'hb_core_anti' not found
Called from: (function()
{
.rs.breakOnError(TRUE)
})()
lapply(testdonors[list(donor_id),],function(donor) calc_donor_yield(1,donor))
Error in NextMethod("[") : object 'hb_core_anti' not found
Called from: (function()
{
.rs.breakOnError(TRUE)
})()
在這兩個調用它看起來像p_donor的只是數據的第一要素。表。
我也試過一個for循環
results <- for (n in 1:nrow(testdonors)) calc_donor_yield(1,testdonors[donorIDlist[n]])
返回NULL
感謝有這方面的幫助。請讓我知道是否有任何其他細節需要幫助得到這個答案。
我什麼calc_heart_yield打算做猜測 - 我想,也許問題是'p_donor [,hb_core_anti == 1 || hb_surface_anti == 1 || hc_anti == 1]'(根據當前環境變量hb_etc,p_donor的值可以給你所有的行和列0或1)是一個非常不同的表達式,例如, 'p_donor $ hb_core_anti == 1 || p_donor $ hb_surface_anti == 1 || p_donor $ hc_anti == 1'(這可能仍然是索引錯誤;我不確定是否在lapply中獲得了一個列表或一行data.frame,這是列表語法)。你打算像後者那樣? –
我通常會發現,準備一個具有所需複雜性的小數據集並使用它通常會讓問題變得更加清晰。目前你有一堆未定義的函數和我們只能猜測的數據。這看起來像你正在傳遞一個單行與lapply:'testdonors [「donor_id」,]'。一般會期望通過一列。 –