[英]R: How to subset multiple elements from a list
> x
[[1]]
[1] "Bob" "John" "Tom"
[2] "Claire" "Betsy"
[[2]]
[1] "Strawberry" "Banana"
[2] "Kiwi"
[[3]]
[1] "Red"
[2] "Blue" "White"
Suppose I had a list x
as shown above. 假设我有一个清单
x
如上所示。 I wish to subset the 2nd element of each entry in the list 我希望对列表中每个条目的第二个元素进行子集化
x[[1]][2]
x[[2]][2]
x[[3]][2]
How can I do that in one command? 如何在一个命令中做到这一点? I tried
x[[1:3]][2]
but I got an error. 我尝试了
x[[1:3]][2]
但出现错误。
Try 尝试
sapply(x2, `[`,2)
#[1] " from localhost (localhost [127.0.0.1])"
#[2] " from phobos [127.0.0.1]"
#[3] " from n20.grp.scd.yahoo.com (n20.grp.scd.yahoo.com"
#[4] " from [66.218.67.196] by n20.grp.scd.yahoo.com with NNFMP;"
x2 <- list(c("Received", " from localhost (localhost [127.0.0.1])"),
c("Received", " from phobos [127.0.0.1]"), c("Received",
" from n20.grp.scd.yahoo.com (n20.grp.scd.yahoo.com"),
c("Received", " from [66.218.67.196] by n20.grp.scd.yahoo.com with NNFMP;" ) )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.