Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
344 views
in Technique[技术] by (71.8m points)

r - How to subset data.frames stored in a list?

I created a list and I stored one data frame in each component. Now I would like to filter those data frames keeping only the rows that have NA in a specific column. I would like the result of this operation to be another list containing data frames with only those rows having NA in that column.

Here is some code to clarify what I am saying. Assume d1 and d2 are my data frames

set.seed(1)

d1<-data.frame(a=rnorm(5), b=c(rep(2006, times=4),NA))
d2<-data.frame(a=1:5, b=c(2007, 2007, NA, NA, 2007))  

print(d1)
 a    b
 1.3011543 2006
 0.3780023 2006
-0.3101449 2006
-1.3927445 2006
-1.0726218   NA

print(d2)
a    b
1 2007
2 2007
3   NA
4   NA
5 2007

which I place in a list with a for loop

ls<-list()

for (i in 1:2){ 

  str<-paste("d", i, sep="")
  dat<-get(str)
  ls[[str]]<-dat

}

Now I would like to filter each list component so to leave only rows of column b that contain NA. To do this I tried using the following command, knowing from the beginning it would have failed. My problem is that I don't know if subset() is the right function to use and, in case it is, I don't know how to qualify each data frame (that is, the first element of the subset function)

lsNA<-lapply(ls, subset(ls, is.na(b)))

Can you please help me get past my severe limitations?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

lapply's second argument is a function (subset) and extra arguments to subset are passed as the ... arguments to lapply. Hence:

my.ls <- list(d1 = d1, d2 = d2)
my.lsNA <- lapply(my.ls, subset, is.na(b))

(I am also showing you how to easily create the list of data.frames without using get, and recommend you don't use ls as a variable name since it is also the name of a rather common function.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...