Skip to content
Back to formatted view

Raw Message

Message-ID: <8d4c23b10911290043o143b80c3n9824fbe4690accd9@mail.gmail.com>
Date: 2009-11-29T08:43:29Z
From: Linlin Yan
Subject: Removing objects from a list based on nrow
In-Reply-To: <131236.80116.qm@web36102.mail.mud.yahoo.com>

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 <mudiver1200 at 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 at 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.
>