[英]Extract a vector from a list
I am trying to cleanly extract a vector from a list. 我试图从列表中干净地提取向量。
The code below provides that data that I want. 下面的代码提供了我想要的数据。 But it returns a list instead of a vector. 但是它返回一个列表而不是向量。
lst_demo <- list(a = c("a1", "a2", "a3"), b = c("b1", "b2"),
c = "c1")
filter_code <- "b"
result <- lst_demo[names(lst_demo) == filter_code]
# result produces what I expect: "b1" , "b2"
result
# but I want the data type to be a vector rather than a list
class(result)
I understand that I can cast to a vector with as.character
but I am looking for a cleaner solution. 我知道可以使用as.character
转换为向量,但是我正在寻找更清洁的解决方案。
Since it's not already added as an answer (but has been said by @thelatemail as a comment) 由于尚未将其添加为答案(但@thelatemail已将其作为评论说)
result <- lst_demo[[filter_code]]
class(result)
[1] "character"
is probably what you're after. 可能正是您所追求的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.