data.table
我有以下内容R
:
library(data.table)
DT = data.table(x=rep(c("b","a","c"),each=3), y=sample(rnorm(9)), v=1:9)
我只想按列计算最小值和最大值,x
并将这两个新列添加到DT
. 这是我的路线:
DT[,c("e","d"):= list(min(y),max(y)), with=FALSE, by = x]
Error in `[.data.table`(DT, , `:=`(c("e", "d"), list(min(y), max(y))), :
'with' must be TRUE when 'by' or 'keyby' is provided
尽管如此,如果我写:DT[,c("e","d"):= list(min(y),max(y)), with=FALSE]
,我会得到:
x y v e d
1: a -1.7125000 4 -1.7125 1.30553
2: a 1.0198038 5 -1.7125 1.30553
3: a 1.3055301 6 -1.7125 1.30553
4: b -0.9238759 1 -1.7125 1.30553
5: b 0.3077016 2 -1.7125 1.30553
6: b -1.2580845 3 -1.7125 1.30553
7: c -0.9399120 7 -1.7125 1.30553
8: c -0.1910583 8 -1.7125 1.30553
9: c 0.1239158 9 -1.7125 1.30553
如您所见,这是有效的,但它并没有完成任务x
。我想获得类似的东西,但e
应该d
由变量的每个值计算x
。所以,我的问题是:我该如何解决这个问题?