【发布时间】:2021-05-16 02:54:32
【问题描述】:
我想对 data.table 应用一个函数,以根据来自其他 data.tables 的两列的评估来添加一个新列。
不知怎的,我无法让它工作:
require(data.table)
# Dummy data. In reality both tables are not identical
DT1 = data.table(
a = c(-1,-1,1,1),
b = c(-1.,1,-1,1)
)
DT2 = data.table(
a = c(-1,-1,1,1),
b = c(-1.,1,-1,1)
)
DT_RES = data.table (id = c(1:4))
check <- function(x1, x2) {
if( (x1<0 && x2>0) || (x1>0 && x2<0)) {return(TRUE)}
else {return(FALSE)}
}
#Try:
DT_RES[,"neg" := check(DT1[,1],DT2[,2])] # No error, but does not return the desired output
# Desired result:
data.table(id = c(1:4), neg=c(FALSE,TRUE,TRUE,FALSE))
为什么它不起作用?
【问题讨论】:
标签: r data.table