【发布时间】:2014-10-15 21:54:38
【问题描述】:
我有一个包含 100,000 行(人)和 500 列(概率)的数据集,我想扫描具有测试概率的列以找到大于和的列标题(a、b 或 c)最接近测试值并将标题记录在新列中。
例如使用 data.table:
data <- data.table( a = seq(0.2, 0.55, length.out = 9),
b = seq(0.35, 0.7, length.out = 9),
c = seq(0.5, 0.85, length.out = 9),
test = seq(0.1, 0.9, length.out = 9)
)
新列将记录第一行的“a”(如 0.1
a b c test
0.20000 0.35000 0.50000 0.1
0.24375 0.39375 0.54375 0.2
0.28750 0.43750 0.58750 0.3
0.33125 0.48125 0.63125 0.4
0.37500 0.52500 0.67500 0.5
0.41875 0.56875 0.71875 0.6
0.46250 0.61250 0.76250 0.7
0.50625 0.65625 0.80625 0.8
0.55000 0.70000 0.85000 0.9
我最初将其作为矩阵而不是 data.table 进行。下面的代码不起作用,但可以了解它是如何操作的
Switch <- pmax(as.matrix(data[,a:c])-matrix(rep(test,3), ncol=3, byrow=F),0)
# subtracts test from columns a,b,c and replaces negative values with 0
FirstSwitch <- Switch[,b:c]>0 & MemSwitch[,a:b]==0
# finds the first non-zero occurance
MonthSwitchMem <- apply(FirstSwitch, 1, which.max)
# calculates the column where the test probability first exceeds
如何跨 data.table 中的列进行匹配。我想我需要使用来自 .SDcols 的查询,但不知道该怎么做?
【问题讨论】:
标签: r match data.table