2012-09-30 110 views
20

我在R中的以下元素的列表:如何提取从列表中的元素混合元素

[[812]] 
[1] ""    "668"   "12345_s_at" "667"   "4.899777748" 
[6] "49.53333333" "10.10930207" "1.598228663" "5.087437057" 

[[813]] 
[1] ""   "376"   "6789_at" "375"   "4.899655078" 
[6] "136.3333333" "27.82508792" "2.20223398" "5.087437057" 

[[814]] 
[1] ""    "19265"  "12351_s_at" "19264"  "4.897730912" 
[6] "889.3666667" "181.5874908" "1.846451572" "5.087437057" 

我知道我可以的情况下,用类似list_elem[[814]][3]访问他们,我想提取第三我需要提取所有列表的第三个元素,例如12345_s_at,并且我想将它们放入向量或列表中,以便稍后将它们的元素与另一个列表进行比较。下面是我的代码:

elem<-(c(listdata)) 
lp<-length(elem) 
for (i in 1:lp) 
{ 
    newlist<-c(listdata[[i]][3]) ###maybe to put in a vector 
    print(newlist) 
} 

当我打印结果我得到的第三个元素,但这样的:

[1] "1417365_a_at" 
    [1] "1416336_s_at" 
    [1] "1416044_at" 
    [1] "1451201_s_at" 

,所以我不能用索引遍历他们像newlist[3],因为它返回NA。我的错误在哪里?

回答

33

如果你想提取每个列表元素的第三个元素,你可以这样做:

List <- list(c(1:3), c(4:6), c(7:9)) 
lapply(List, '[[', 3) # This returns a list with only the third element 
unlist(lapply(List, '[[', 3)) # This returns a vector with the third element 

使用你的榜样,并考虑到@GSee评论,你可以这样做:

yourList <- list(c("","668","12345_s_at","667", "4.899777748","49.53333333", 
     "10.10930207", "1.598228663","5.087437057"), 
    c("","376", "6789_at", "375", "4.899655078","136.3333333", 
     "27.82508792", "2.20223398", "5.087437057"), 
    c("", "19265", "12351_s_at", "19264", "4.897730912", 
     "889.3666667", "181.5874908","1.846451572","5.087437057")) 

sapply(yourList, '[[', 3) 
[1] "12345_s_at" "6789_at" "12351_s_at" 

下一次您可以在数据集的一部分上使用dput提供一些数据,以便我们轻松地重现您的问题。

+7

使用'sapply'来避免'unlist'部分。另外,我认为'['就足够了。 +1 – GSee

0

如果你想用你在你的问题中键入的代码,下面是修复:

listdata <- list(c("","668","12345_s_at","667", "4.899777748","49.53333333", 
     "10.10930207", "1.598228663","5.087437057"), 
    c("","376", "6789_at", "375", "4.899655078","136.3333333", 
     "27.82508792", "2.20223398", "5.087437057"), 
    c("", "19265", "12351_s_at", "19264", "4.897730912", 
     "889.3666667", "181.5874908","1.846451572","5.087437057")) 

v <- character() #creates empty character vector 
list_len <- length(listdata) 
for(i in 1:list_len) 
    v <- c(v, listdata[[i]][3]) #fills the vector with list elements (not efficient, but works fine) 

print(v) 
[1] "12345_s_at" "6789_at" "12351_s_at" 
6

随着purrr可以提取元素,并确保数据类型的一致性:

library(purrr) 

listdata <- list(c("","668","12345_s_at","667", "4.899777748","49.53333333", 
     "10.10930207", "1.598228663","5.087437057"), 
    c("","376", "6789_at", "375", "4.899655078","136.3333333", 
     "27.82508792", "2.20223398", "5.087437057"), 
    c("", "19265", "12351_s_at", "19264", "4.897730912", 
     "889.3666667", "181.5874908","1.846451572","5.087437057")) 

map_chr(listdata, 3) 
## [1] "12345_s_at" "6789_at" "12351_s_at" 

有其他的map_函数也强制执行类型一致性,以及最终可以帮助结束do.call(rbind, …)疯狂的map_df()

+0

死简单。这是对这个问题的现代/整洁的答案。 – Andrew