[R] Help with data.frame subsets

Tony Plate tplate at blackmesacapital.com
Tue Mar 25 23:54:47 CET 2003


The "[" operator also takes a drop= argument (type ?"[" to see the help page and find details.)

So you can do what you want in a single step, as follows:

subData <- dataFrame[dataFrame[,2]>=Min & dataFrame[,2]<=Max, , drop=F]

[If your "dataFrame" object really is a data.frame, the drop=F shouldn't be necessary -- maybe you have actually have a matrix and not a data frame, because the default behavior for dataframes is to keep the result as a dataframe when the result contains only one row, in contrast to the default behavior for matrices, which is to drop any dimension that has only one element.

> x <- data.frame(a=1:3,b=2:4)
> x
  a b
1 1 2
2 2 3
3 3 4
> x[1,] # default behavior of [ on data.frame is to not drop when have a single row
  a b
1 1 2
> class(x[1,])
[1] "data.frame"
> x[1,,drop=T] # returns a list
$a
[1] 1

$b
[1] 2

> xx <- as.matrix(x)
> xx[1,] # returns a vector
a b 
1 2 
> dim(xx[1,,drop=F]) # returns a matrix
[1] 1 2
> dim(xx[1,])
NULL
>


]


At Tuesday 04:01 PM 3/25/2003 -0500, 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