Column in the j-expression of a data.table (with/without a by statement)

橙三吉。 提交于 2019-12-06 12:28:43

1) Use .N. The length of the grouping variable A there is 1 because there is just one value of A for each group (this is by definition of what grouping means):

dt <- data.table(A=c("a","a","b"), B="")
dt[, .N, by = A]
#   A N
#1: a 2
#2: b 1

(updated 1) This is the same issue as 2). A workaround is to not use .SDcols:

dt = data.table(A=c("a","a","b"), H=c(0,1,-1), x=c(1,2,3), y=c(9,8,7))
dt[, lapply(.SD[, !"H", with = F], function(x) mean(x) ^ mean(H)), by = A]
#   A         x         y
#1: a 1.2247449 2.9154759
#2: b 0.3333333 0.1428571

2) This is a bug that's been reported before here: https://r-forge.r-project.org/tracker/index.php?func=detail&aid=5222&group_id=240&atid=975

I don't know if I understand you correctly.

1)

library(data.table)
dat0 <- data.frame(A=c("a","a","b"), B="")
data.table(dat0)[, list(l= nrow(.SD)) , by = "A"]

result:

   A l
1: a 2
2: b 1

2)

dat0 <- data.frame(A=c("a","a","b"), B=1:3, c=0)
data.table(dat0)[, list(c=unlist(.SD)), .SDcols= "B"]

result:

   c
1: 1
2: 2
3: 3

1')

Edit: I changed -1 to mycols

dat0 <- data.frame(A=c("a","a","b"), H=c(0,1,-1), x=c(1,2,3), y=c(9,8,7))

mycols = c("x", "y")
data.table(dat0)[, lapply(.SD[,mycols,with=F], function(x) mean(x)^mean(H)) ,by = "A", .SDcols = c("H", mycols)]

result:

   A         u         v
1: a 1.2247449 2.9154759
2: b 0.3333333 0.1428571
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!