gpt4 book ai didi

r - 将函数应用于列表列表的有效方法

转载 作者:行者123 更新时间:2023-12-04 11:02:05 26 4
gpt4 key购买 nike

我有 80 个相关项目的列表。每个列表都是一个长度为 1000 的列表。我想对每个列表(每个 1000 个)运行一个函数,并将结果分配回原始对象。总数据超过 150 个演出,所以我想确保在实际数据上运行它之前这是最有效的。这个微不足道的例子是做我需要的最好的方法吗?

# my actual function is obviously more complicated.
# But let's say the goal is to keep 2/5 items in each list
trivial <- function(foo) {
keep <- c("S1", "S2")
foo[which(keep %in% names(foo))]
}

sublist <- replicate(5, as.list(1:5), simplify=FALSE)
names(sublist) <- paste0("S", 1:5)
eachlist <- replicate(5, sublist, simplify = F)
a1 <- a2 <- a3 <- a4 <- a5 <- eachlist

# To clarify the layout
length(a1)
[1] 5
> length(a1[[1]])
[1] 5
> names(a1[[1]])
[1] "S1" "S2" "S3" "S4" "S5"
# I need to drop S3-S5 from each of 5 sublists of a1.
# Now I'd like to repeat this for all 80 lists named a[0-9].


# all the objects have a pattern sometextNUMBER. This list is
# just the names of all the lists.
listz <- as.list(ls(pattern="[a-z][0-9]"))
> listz
[[1]]
[1] "a1"

[[2]]
[1] "a2"

[[3]]
[1] "a3"

[[4]]
[1] "a4"

[[5]]
[1] "a5"
# I don't need anything returned, just for a1-a80 updated such that
# in each sublist, 3 of 5 items are dropped.

# This works fine, but my concern now is just scaling this up.
l_ply(listz, function(x){
assign(as.character(x), llply(get(x), trivial), envir = .GlobalEnv)
})

最佳答案

您可以使用 substitute() 遍历名称列表。和 eval()首先构造然后执行您(不是!)喜欢在命令行中单独键入的表达式:

objNames <- ls(pattern="[a-z][0-9]")

for(objName in objNames) {
expr <-
substitute({
OBJ <- lapply(OBJ, function(X) X[names(X) %in% c("S1", "S2")])
}, list(OBJ = as.symbol(objName)))
eval(expr)
}

关于r - 将函数应用于列表列表的有效方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12938765/

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