Jim, Good catch! I know in my current problem there are objects with less than 3 rows, but will make sure to modify the function for future use.
Thanks, Tim Tim Clark Department of Zoology University of Hawaii --- On Sun, 11/29/09, Linlin Yan <yanlinli...@gmail.com> wrote: > From: Linlin Yan <yanlinli...@gmail.com> > Subject: Re: [R] Removing objects from a list based on nrow > To: "jim holtman" <jholt...@gmail.com> > Cc: "Tim Clark" <mudiver1...@yahoo.com>, r-help@r-project.org > Date: Sunday, November 29, 2009, 4:35 AM > Thank Jim! You are right. I didn't > notice the case of none of rows > match the condition. > > On Sun, Nov 29, 2009 at 10:10 PM, jim holtman <jholt...@gmail.com> > wrote: > > One thing to be careful of is if no dataframe have > less than 3 rows: > > > >> > df1<-data.frame(letter=c("A","B","C","D","E"),number=c(1,2,3,4,5)) > >> > df2<-data.frame(letter=c("A","B"),number=c(1,2)) > >> > df3<-data.frame(letter=c("A","B","C","D","E"),number=c(1,2,3,4,5)) > >> > df4<-data.frame(letter=c("A","B","C","D","E"),number=c(1,2,3,4,5)) > >> > >> lst<-list(df1,df3,df4) > >> lst > > [[1]] > > letter number > > 1 A 1 > > 2 B 2 > > 3 C 3 > > 4 D 4 > > 5 E 5 > > > > [[2]] > > letter number > > 1 A 1 > > 2 B 2 > > 3 C 3 > > 4 D 4 > > 5 E 5 > > > > [[3]] > > letter number > > 1 A 1 > > 2 B 2 > > 3 C 3 > > 4 D 4 > > 5 E 5 > > > >> lst[-which(sapply(lst, nrow) < 3)] > > list() > >> > > > > Notice the list is now empty. Instead use: > > > >> lst[sapply(lst, nrow) >=3] > > [[1]] > > letter number > > 1 A 1 > > 2 B 2 > > 3 C 3 > > 4 D 4 > > 5 E 5 > > > > [[2]] > > letter number > > 1 A 1 > > 2 B 2 > > 3 C 3 > > 4 D 4 > > 5 E 5 > > > > [[3]] > > letter number > > 1 A 1 > > 2 B 2 > > 3 C 3 > > 4 D 4 > > 5 E 5 > > > > > > On Sun, Nov 29, 2009 at 3:43 AM, Linlin Yan <yanlinli...@gmail.com> > wrote: > >> Try these: > >> sapply(lst, nrow) # get row numbers > >> which(sapply(lst, nrow) < 3) # get the index of > rows which has less than 3 rows > >> lst <- lst[-which(sapply(lst, nrow) < 3)] # > remove the rows from the list > >> > >> On Sun, Nov 29, 2009 at 4:36 PM, Tim Clark <mudiver1...@yahoo.com> > wrote: > >>> Dear List, > >>> > >>> I have a list containing data frames of > various numbers of rows. I need to remove any data frame > that has less than 3 rows. For example: > >>> > >>> > df1<-data.frame(letter=c("A","B","C","D","E"),number=c(1,2,3,4,5)) > >>> > df2<-data.frame(letter=c("A","B"),number=c(1,2)) > >>> > df3<-data.frame(letter=c("A","B","C","D","E"),number=c(1,2,3,4,5)) > >>> > df4<-data.frame(letter=c("A","B","C","D","E"),number=c(1,2,3,4,5)) > >>> > >>> lst<-list(df1,df2,df3,df4) > >>> > >>> How can I determine that the second object > (df2) has less than 3 rows and remove it from the list? > >>> > >>> Thanks! > >>> > >>> Tim > >>> > >>> > >>> > >>> > >>> Tim Clark > >>> Department of Zoology > >>> University of Hawaii > >>> > >>> > ______________________________________________ > >>> R-help@r-project.org > mailing list > >>> https://stat.ethz.ch/mailman/listinfo/r-help > >>> PLEASE do read the posting guide > >>> http://www.R-project.org/posting-guide.html > >>> and provide commented, minimal, > self-contained, reproducible code. > >>> > >> > >> ______________________________________________ > >> R-help@r-project.org > mailing list > >> https://stat.ethz.ch/mailman/listinfo/r-help > >> PLEASE do read the posting guide > >> http://www.R-project.org/posting-guide.html > >> and provide commented, minimal, self-contained, > reproducible code. > >> > > > > > > > > -- > > Jim Holtman > > Cincinnati, OH > > +1 513 646 9390 > > > > What is the problem that you are trying to solve? > > > ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.