[R] Help with data.frame subsets

Stephen C. Upton upton at mitre.org
Tue Mar 25 22:18:57 CET 2003


Dave,

Look at subset. This function retains the data as a data frame.  You can do
both your operations at once also.

HTH
steve

David Thibault wrote:

> 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,
> Dave
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help



More information about the R-help mailing list