gpt4 book ai didi

r - 拆分data.table,然后通过引用修改

转载 作者:行者123 更新时间:2023-12-04 10:45:27 24 4
gpt4 key购买 nike

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

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

#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 的方法?请注意,在实际情况下,我想以编程方式通过引用进行修改,因此对解决方案进行硬编码是行不通的。

最佳答案

这是使用 .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)

关于r - 拆分data.table,然后通过引用修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28533804/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com