我写了一个基于条件提取行的函数,如下所示:
ex <- function(x, y){
Sname <- paste0("A00-",x)
state <- paste0(x, "_state")
test1 <- paste0(x, "_test1")
test2 <- paste0(x, "_test2")
temp <- dt[c("ID",state,test1,test2)
temp[,test1] <- ifelse(temp[,state] >= y, temp[,test1] ,"NA")
head(temp)]
}
我的表有列
> names(dt)
[1] "ID" "A_test1" "A_test2" "A_state" "A_anoo"
[6] "B_test1" "B_test2" "B_state" "B_anoo" "C_test1"
[11] "C_test2" "C_state" "C_anoo"
当我使用我的函数ex(x = "B", y= 0.2)
时,我会得到这个错误:
> temp <- dt[c("ID",state,test1,test2)]
Error in `[.data.table`(dt, c("ID",state,test1,test2), ) :
When i is a data.table (or character vector), the columns to join by must be specified using 'on=' argument (see ?data.table), by keying x (i.e. sorted, and, marked as sorted, see ?setkey), or by sharing column names between x and i (i.e., a natural join). Keyed joins might have further speed benefits on very large data due to x being sorted in RAM.
如果有人能帮我,那就太好了。谢谢。
1条答案
按热度按时间ia2d9nvy1#
您的问题与data.table中的非标准评估有关。
我还建议你研究一下数据、表格插图和文档,特别是关于
:=
(通过引用分配)和fifelse
。应避免混合数据表和基础R(子)赋值。