簡體   English   中英

將函數應用於列表列表的有效方法

[英]Efficient way to apply a function to a list of lists

我有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)
}

這是rapply一個很好的用例:

listz <- replicate(5, as.list(1:5), simplify=FALSE)
fun <- function(x) x*10
out <- rapply(listz, fun, how="replace")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM