7

如果我使用bydata.table 中的关键字进行分组,它总是将该by列作为第一列返回。是否有一个标志/选项告诉它不要这样做?还是摆脱它的聪明方法?

特别是我想分组然后rbindlist到我的原始表,所以事实上这个问题也可以说是 - “如何阻止它重新排序列”

例如:

DT = data.table(I = as.numeric(1:6), N = rnorm(6), L = rep(c("a", "b", "c"), 2))
DT[, list(I = mean(I), N = mean(N)), by= L]
DT

给出:

> DT[, list(I = mean(I), N = mean(N)), by= L]
   L   I          N
1: a 2.5  0.4291802
2: b 3.5  0.6669517
3: c 4.5 -0.6471886
> DT
   I          N L
1: 1  1.8460998 a
2: 2  0.7093438 b
3: 3 -1.7991193 c
4: 4 -0.9877394 a
5: 5  0.6245596 b
6: 6  0.5047421 c

rbindlist请求而言,能够做到这一点会很好:

DT = rbindlist(list(DT, DT[, list(I = mean(I), N = mean(N)), by= L]))

也许

DT = rbindlist(list(DT, DT[, list(I = mean(I), N = mean(N), L), by= L]))

或类似的东西(两者都不起作用)

4

1 回答 1

4

我也不是特别喜欢这种自动列重新排序。我通常做的“技巧”是setcolorder在获得输出后使用如下:

DT <- data.table(I = 1:6, N = rnorm(6), L = rep(c("a", "b", "c"), 2))
DT.out <- DT[, list(I = mean(I), N = mean(N)), by= L]

在这里,setcolorder如:

setcolorder(DT.out, names(DT))

#      I            N L
# 1: 2.5  0.772719306 a
# 2: 3.5 -0.008921738 b
# 3: 4.5 -0.770807996 c

当然,如果 的名称DT与 相同,则此方法有效DT.out。否则,您必须将列顺序明确指定为:

setcolorder(DT.out, c("I", "N", "L"))

编辑:由于您想立即按行绑定它们,是的,最好不要将其作为中间结果。而且由于rbindlist似乎按位置绑定,您可以使用rbindwhich 通过列名绑定并将data.table其作为警告并建议使用use.names=F,如果您想改为按位置绑定。您可以放心地忽略此警告。

dt1 <- data.table(x=1:5, y=6:10)
dt2 <- data.table(y=1:5, x=6:10)

rbind(dt1, dt2) # or do.call(rbind, list(dt1, dt2))

#      x  y
#  1:  1  6
#  2:  2  7
#  3:  3  8
#  4:  4  9
#  5:  5 10
#  6:  6  1
#  7:  7  2
#  8:  8  3
#  9:  9  4
# 10: 10  5
# Warning message:
# In .rbind.data.table(...) :
#   Argument 2 has names in a different order. Columns will be bound by name for 
#   consistency with base. Alternatively, you can drop names (by using an unnamed 
#   list) and the columns will then be joined by position. Or, set use.names=FALSE.
于 2013-02-23T11:00:27.643 回答