簡體   English   中英

遍歷列表以通過其名稱獲取價值

[英]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.

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