Skip to content
Back to formatted view

Raw Message

Message-ID: <77EB52C6DD32BA4D87471DCD70C8D700043B745D@NA-PA-VBE03.na.tibco.com>
Date: 2011-05-17T20:39:07Z
From: William Dunlap
Subject: subsetting a list of dataframes
In-Reply-To: <BANLkTimS73dDE5gCWyY=zxkM03Dpf+3Dew@mail.gmail.com>

shouldKeep <- sapply(listOfDataFrames, function(df)nrow(df)>1)
  listOfDataFrames[shouldKeep]
or, compressed to get rid of the intermediate variable
  listOfDataFrames[sapply(listOfDataFrames, function(df)nrow(df)>1)]

If you are writing production code and there is any chance that
listOfDataFrames might be an empty list you can use vapply (which
requires that you supply a prototype for FUN's return value):
  listOfDataFrames[vapply(listOfDataFrames, function(df)nrow(df)>1,
FUN.VALUE=FALSE)]


Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com  

> -----Original Message-----
> From: r-help-bounces at r-project.org 
> [mailto:r-help-bounces at r-project.org] On Behalf Of Lara Poplarski
> Sent: Tuesday, May 17, 2011 1:25 PM
> To: r-help at r-project.org
> Subject: [R] subsetting a list of dataframes
> 
> Hello All,
> 
> I have a list of dataframes, and I need to subset it by 
> keeping only those
> dataframes in the list that meet a certain criterion. 
> Specifically, I need
> to generate a second list which only includes those 
> dataframes whose number
> of rows is > 1.
> 
> Could someone suggest how to do this? I have come close to 
> what I need with
> loops and such, but there must be a less clumsy way...
> 
> Many thanks,
> Lara
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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.
>