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
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
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) > xa 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 rowa 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 vectora 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
Maybe Matching Threads
- How to apply a function to subsets of a data frame *and* obtain a data frame again?
- Eliminate cases in a subset of a dataframe
- Problem to remove loops in a routine
- Extracting values from Surv function in survival package
- Extracting values from Surv function in survival package