拆分data.table,然后通过引用进行修改

时间:2015-02-16 02:41:00

标签: r data.table

我有一个用例,我需要拆分data.table,然后对每个分区应用不同的引用修改操作。但是,拆分会强制复制每个表。

这是虹膜数据集上的一个玩具示例:

#split the data
DT <- data.table(iris)
out <- split(DT, DT$Species)

#assign partitions to global environment
NAMES <- as.character(unique(DT$Species))
lapply(seq_along(out), function(x) {
assign(NAMES[x], out[[x]], envir=.GlobalEnv)})

#modify by reference, same function applied to different columns for different partitions
#would do this programatically in real use case
virginica[ ,summ:=sum(Petal.Length)]
setosa[ ,summ:=sum(Petal.Width)]

#rbind all (again, programmatic)
do.call(rbind, list(virginica, setosa))

然后我收到以下警告:

 Warning message:
 In `[.data.table`(out$virginica, , `:=`(cumPedal, cumsum(Petal.Width))) :
  Invalid .internal.selfref detected and fixed by taking a copy of the whole table so that := can add this new column by reference.

我知道这与将data.tables放在列表中有关。是否有针对此用例的解决方法,或避免使用split的方法?请注意,在实际情况中,我想以编程方式通过引用进行修改,因此对解决方案进行硬编码将无法正常工作。

1 个答案:

答案 0 :(得分:4)

以下是使用.EACHI来实现您尝试做的事情的示例:

## Create a data.table that indicates the pairs of keys to columns
New <- data.table(
  Species = c("virginica", "setosa", "versicolor"), 
  FunCol = c("Petal.Length", "Petal.Width", "Sepal.Length"))

## Set the key of your original data.table
setkey(DT, Species)

## Now use .EACHI
DT[New, temp := cumsum(get(FunCol)), by = .EACHI][]
#      Sepal.Length Sepal.Width Petal.Length Petal.Width   Species  temp
#   1:          5.1         3.5          1.4         0.2    setosa   0.2
#   2:          4.9         3.0          1.4         0.2    setosa   0.4
#   3:          4.7         3.2          1.3         0.2    setosa   0.6
#   4:          4.6         3.1          1.5         0.2    setosa   0.8
#   5:          5.0         3.6          1.4         0.2    setosa   1.0
#  ---                                                                  
# 146:          6.7         3.0          5.2         2.3 virginica 256.9
# 147:          6.3         2.5          5.0         1.9 virginica 261.9
# 148:          6.5         3.0          5.2         2.0 virginica 267.1
# 149:          6.2         3.4          5.4         2.3 virginica 272.5
# 150:          5.9         3.0          5.1         1.8 virginica 277.6

## Basic verification
head(cumsum(DT["setosa", ]$Petal.Width), 5)
# [1] 0.2 0.4 0.6 0.8 1.0
tail(cumsum(DT["virginica", ]$Petal.Length), 5)
相关问题