Allen S. Rout
2006-Jun-20 19:52 UTC
[R] Sort of data.frame yields a thing which is not a data.frame...
I've observed something I don't understand, and I was hoping someone could point me to the right section of docs. There's a portion in one of my analyses in which I am wont to sort a data.frame so: seriesS <- seriesS[order(as.Date(row.names(seriesS),format="%m/%d/%Y")),] So, I've got row.names which are textual representations of dates, I'd like to retain them as such, but order them datewise. As long as seriesS has more than one column, this works nicely, seriesS afterwards is a data.frame with columns similar to those it had going in. But if I encounter a case with only one column, the result is _not_ a data frame, but instead an ? array? list? I can solve this with a kluge: seriesS$stupid <- 0; seriesS <- seriesS[order(as.Date(row.names(seriesS),format="%m/%d/%Y")),] seriesS <- seriesS[,-c(which(names(seriesS)=="stupid")) ] but this mostly tells me I've failed to understand something about how the process should work. Any good references to the Chapter and Verse of the Canon of R I should hit? - Allen S. Rout
Per Jensen
2006-Jun-20 20:05 UTC
[R] Sort of data.frame yields a thing which is not a data.frame...
Compare d<-data.frame(a=c(1,2),b=c(2,3))> class(d[,1])[1] "numeric"> class(d[,1,drop=FALSE])[1] "data.frame" Regards On 20 Jun 2006 15:52:36 -0400, Allen S. Rout <asr@ufl.edu> wrote:> > > > I've observed something I don't understand, and I was hoping someone > could point me to the right section of docs. > > There's a portion in one of my analyses in which I am wont to sort a > data.frame so: > > seriesS <- seriesS[order(as.Date(row.names(seriesS),format="%m/%d/%Y")),] > > So, I've got row.names which are textual representations of dates, I'd > like to retain them as such, but order them datewise. > > > As long as seriesS has more than one column, this works nicely, > seriesS afterwards is a data.frame with columns similar to those it > had going in. But if I encounter a case with only one column, the > result is _not_ a data frame, but instead an ? array? list? > > I can solve this with a kluge: > > seriesS$stupid <- 0; > seriesS <- seriesS[order(as.Date(row.names(seriesS),format="%m/%d/%Y")),] > seriesS <- seriesS[,-c(which(names(seriesS)=="stupid")) ] > > but this mostly tells me I've failed to understand something about how > the process should work. > > > Any good references to the Chapter and Verse of the Canon of R I > should hit? > > > > - Allen S. Rout > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! > http://www.R-project.org/posting-guide.html >[[alternative HTML version deleted]]