[英]Iterate over list to get value by its name
這可行。 但是,是否有更有效/更簡單的方法來獲取output
?
test_list <- list(list("name"="A","property"=1),
list("name"="B","property"=2),
list("name"="C","property"=3))
myFunction <- function(arg1=NULL, arg2=NULL){
arg1[[arg2]]
}
# works
output <- sapply(test_list, myFunction, "property")
# returns NULL
# output <- sapply(test_list, `$`, "property")
我們可以指定匿名函數調用來進行提取
sapply(test_list, function(x) x$property)
#[1] 1 2 3
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.