list 如何从具有混合元素的列表中提取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12662005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to extract elements from a list with mixed elements
提问by Layla
I have a list in R with the following elements:
我在 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"
I know I can access them with something like list_elem[[814]][3]
in case that I want to extract the third element of the position 814.
I need to extract the third element of all the list, for example 12345_s_at
, and I want to put them in a vector or list so I can compare their elements to another list later on. Below is my code:
我知道我可以使用类似的方法访问它们,list_elem[[814]][3]
以防我想提取位置 814 的第三个元素。例如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)
}
When I print the results I get the third element, but like this:
当我打印结果时,我得到了第三个元素,但像这样:
[1] "1417365_a_at"
[1] "1416336_s_at"
[1] "1416044_at"
[1] "1451201_s_at"
so I cannot traverse them with an index like newlist[3]
, because it returns NA
. Where is my mistake?
所以我不能用像 的索引遍历它们newlist[3]
,因为它返回NA
. 我的错误在哪里?
回答by Jilber Urbina
If you want to extract the third element of each list element you can do:
如果要提取每个列表元素的第三个元素,可以执行以下操作:
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
Using your example and taking into account @GSee comment you can do:
使用您的示例并考虑到@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"
Next time you can provide some data using dput
on a portion of your dataset so we can reproduce your problem easily.
下次您可以使用dput
部分数据集提供一些数据,以便我们可以轻松重现您的问题。
回答by hrbrmstr
With purrr
you can extract elements and ensure data type consistency:
随着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"
There are other map_
functions that enforce the type consistency as well and a map_df()
which can finally help end the do.call(rbind, …)
madness.
还有其他map_
函数也可以强制类型一致性,并且map_df()
最终可以帮助结束do.call(rbind, …)
疯狂。
回答by Marco Demaio
In case you wanted to use the code you typed in your question, below is the fix:
如果您想使用您在问题中输入的代码,以下是修复程序:
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"