[R] Help with data.frame subsets

David Thibault dthibault at esperion.com
Tue Mar 25 22:01:24 CET 2003

Hello all,

I'm trying to get a subset of a data frame by taking all rows where the 2nd
column is >= Min and <= Max.  I can do that by a 2 step process similar to
the following:

subData <- dataFrame[dataFrame[,2] >= Min,]
subData2 <- subData[subData[,2] <= Max,]

Then I try to graph the results where col 2 is the X var and col 3 is the Y
var.  Therefore I do the following:
X <- subData2[,2]
Y <- subData2[,3]

HOWEVER, sometimes subData2 is only left with 1 row remaining after the
subsetting operation.  If it gets down to that point R seems to return the
results as a LIST and not as a data frame.  Then when I do the X and Y
assignments above it gives the following error:

Error in subData2[, 2] : incorrect number of dimensions

I can't always assume that subData2 will be a list because most of the time
multiple rows are left in the subset and therefore it's still a data frame.
Does anyone have a solution for handling this kind of operation that won't
crash on me half the time?

Thanks in advance,

More information about the R-help mailing list