R 迭代列表以按其名称获取值

R 迭代列表以按其名称获取值,r,list,iteration,key-value,sapply,R,List,Iteration,Key Value,Sapply,这很有效。但是有没有更有效/更简单的方法来获得输出 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

这很有效。但是有没有更有效/更简单的方法来获得
输出

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")

test\u list我们可以指定匿名函数调用来进行提取

sapply(test_list, function(x) x$property)
#[1] 1 2 3

您可以使用
sappy(test\u list,function(x)x$property)
sappy(test\u list,[[”,“property”)
sappy(test\u list,getElement,name=“property”)
我遗漏了x参数。谢谢。通常应该避免在函数中使用“$”,因此更好的答案应该是:
function(x)x['property']