处理存储在列表中的数据框 [英] Work with data frames stored in lists
问题描述
关于这个问题,我想在此处开始并扩展讨论.这是为了像我这样在必须使用lapply时仍然遇到麻烦的人的利益.
With this question I would like to extend and generalize the discussion started here. This is for the benefit of those, like me, who are still in trouble when have to use lapply.
假设我有数据帧d1
和d2
,它们存储在列表my.ls
Suppose I have the data frames d1
and d2
which I store in the list my.ls
d1<-data.frame(a=rnorm(5), b=c(rep(2006, times=4),NA), c=letters[1:5])
d2<-data.frame(a=1:5, b=c(2007, 2007, NA, NA, 2007), c=letters[6:10])
my.ls<-list(d1=d1, d2=d2)
如何获得另一个具有相同数据框的列表,而仅保留第一列和第三列?我尝试了以下方法,但是没有用
How can I obtain another list featuring the same data frames for which I keep only the first and third columns? I tried the following, but it didn't work
my.ls.sub<-lapply(my.ls, my.ls[,c(1,3)])
然后,如果我不仅想对数据帧进行子集化,还想知道我提取的列中的unique
值是什么? (换句话说,在这里我将为每个数据帧创建两个向量,这些向量可以是自由的,也可以存储在列表列表中).对于后一点,我无能为力...
What if then, I not only want to subset the data frames, but I also want to know what are the unique
values in the columns I am extracting? (In other words, here I would create two vectors for every data frame which could be free or stored in a list of lists). For the latter point I am not able to suggest anything...
推荐答案
尝试一下
lapply(my.ls, "[", ,c(1,3))
或编辑一点代码即可得到:
Or editing a little bit your code yields:
lapply(my.ls, function(x) x[, c(1,3)])
编辑
由于@Matthew Plourde已经使用lapply
回答了问题的第二部分,所以我为您提供了使用rapply
的另一种方法,它是lapply
的递归版本.
Edit
Since @Matthew Plourde already answered the second part of your question using lapply
, then I give you an alternative way to do it using rapply
which is the recursive version of lapply
.
rapply(lapply(my.ls, "[", ,c(1,3)), unique, how="list")
这篇关于处理存储在列表中的数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!