[英]r data.table lapply with multiple SDcols
I am trying to use an lapply that takes two lists of columns into account (all are numeric type) in what would seem to be a simple line of code, but the output is not as expected.我正在尝试使用一个 lapply,它在看似简单的代码行中考虑了两个列列表(均为数字类型),但 output 与预期不同。
This is my code:这是我的代码:
x<-50
measure <- c("haz", "waz", "whz", "htcm", "wtkg", "bmi")
new_measure_1.5 <- paste(measure, "1.5", sep = "_")
new_temp_cols<-paste("temp", new_measure_1.5, sep = "_")
new_columns<-paste(new_measure_1.5, "1", sep="_")
newcols_1.5_months<-function(x, agedays, new_temp_cols, y){
ifelse(agedays==x, new_temp_cols, y)
}
DT[, (new_columns) := lapply(.SD, function(y) newcols_1.5_months(x, agedays, new_temp_cols, y)), .SDcols = new_columns ]
The above code results in new_columns (haz_1.5_1, waz_1.5_1, whz_1.5_1, wtkg_1.5_1, htcm_1.5_1, bmi_1.5_1) holding the names of the columns in the list new_temp_cols (temp_haz_1.5, temp_waz_1.5, temp_whz_1.5, temp_wtkg_1.5, temp_htcm_1.5, temp_bmi_1.5) as opposed to the values they hold in my data table.上述代码导致 new_columns (haz_1.5_1, waz_1.5_1, whz_1.5_1, wtkg_1.5_1, htcm_1.5_1, bmi_1.5_1) 包含列表 new_temp_cols (temp_haz_1.5, temp_waz_1.5, temp_whz_1.5, temp_wtkg_1.5, temp_htcm_1.5, temp_bmi_1.5) 而不是它们在我的数据表中保存的值。 It seems that R is reading the list as a vector of strings rather than a vector of columns.
似乎 R 正在将列表作为字符串向量而不是列向量读取。 Why is this?
为什么是这样?
I've tried using multiple.SDcols, but this doesn't work:我试过使用 multiple.SDcols,但这不起作用:
DT[, (new_columns) := lapply(.SD, function(y) newcols_1.5_months(x, agedays, new_temp_cols, y)), .SDcols = c(new_columns, new_temp_cols) ]
Is there a simple fix to this?有一个简单的解决方法吗?
**** Editing to add a small subset of dummy data similar to my data table **** 编辑以添加类似于我的数据表的一小部分虚拟数据
measure<-c("haz", "waz")
new_measure_1.5 <- paste(measure, "1.5", sep = "_")
new_temp_cols<-paste("temp", new_measure_1.5, sep = "_")
new_columns<-paste(new_measure_1.5, "1", sep="_")
anthro <- data.table
(agedays = c(25,50,53,22,37,50,12,45,50,15,33,50),
temp_haz_1.5 = c(1.2,1.5,1.7,2.0,4.5,6.7,6.8,6.7,4.5,6.6,8.9,6.7),
temp_waz_1.5 = c(3.2,1.8,6.7,2.8,3.5,7.7,9.8,1.7,6.9,3.8,0.9,4.7),
haz_1.5_1 = c(1.2,2.5,4.7,7.0,4.7,6.8,6.3,2.7,5.5,8.6,3.9,6.7),
waz_1.5_1 =c(6.2,2.5,5.7,7.0,2.5,7.7,8.8,9.7,2.5,4.6,5.9,6.7))
If we have multiple columns, then use Map
to loop over each of the corresponding sets of columns and apply the function.如果我们有多个列,则使用
Map
循环遍历每组相应的列并应用 function。
library(data.table)
x <- 50
DT[, (new_columns) := Map(function(u, y)
newcols_1.5_months(u, DT[['agedays']], x, y),
.SD[, new_columns, with = FALSE],
.SD[, new_temp_cols, with = FALSE]) ]
Perhaps the function can be也许 function 可以
newcols_1.5_months<- function(u, agedays, x, y){
ifelse(agedays==x, u, y)
}
DT <- data.table(agedays = c(25,50,53,22,37,50,12,45,50,15,33,50),
temp_haz_1.5 = c(1.2,1.5,1.7,2.0,4.5,6.7,6.8,6.7,4.5,6.6,8.9,6.7),
temp_waz_1.5 = c(3.2,1.8,6.7,2.8,3.5,7.7,9.8,1.7,6.9,3.8,0.9,4.7),
haz_1.5_1 = c(1.2,2.5,4.7,7.0,4.7,6.8,6.3,2.7,5.5,8.6,3.9,6.7),
waz_1.5_1 =c(6.2,2.5,5.7,7.0,2.5,7.7,8.8,9.7,2.5,4.6,5.9,6.7))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.