Skip to content
Prev 260146 / 398502 Next

subsetting a list of dataframes

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