Dear R users, I have a data frame which I create with read.csv and then order by date: d <- na.omit(read.csv(...)) d <- d[order(as.Date(as.character(d$Date), format="%d-%b-%y"), decreasing=F, na.last=F),] My problem is that even though the data frame is ordered as requested, the old row numbers are preserved. For example: * Before sorting:> d[1:3,]Date Amt 1 5-Nov-04 87.07 2 4-Nov-04 85.80 3 3-Nov-04 82.90 * After sorting:> d[1:3,]Date Amt 500 12-Nov-02 84.23 499 13-Nov-02 85.05 498 14-Nov-02 84.95 Is there a way to update the row numbers as well? It's not that important, but I find it a bit confusing. Thank you, b.
On Mon, 8 Nov 2004, bogdan romocea wrote:> My problem is that even though the data frame is ordered as > requested, the old row numbers are preserved. For example: > > * Before sorting: >> d[1:3,] > Date Amt > 1 5-Nov-04 87.07 > 2 4-Nov-04 85.80 > 3 3-Nov-04 82.90 > > * After sorting: >> d[1:3,] > Date Amt > 500 12-Nov-02 84.23 > 499 13-Nov-02 85.05 > 498 14-Nov-02 84.95 > > Is there a way to update the row numbers as well? It's not that > important, but I find it a bit confusing. >This is an important feature of R: the row names of a data frame stay fixed under subsetting or reordering. You can change the rownames with eg rownames(d)<-1:nrow(d) if you want to. -thomas
It's your misinterpretion that is misleading, not the output. Data frames have row *names* and not *numbers*. On Mon, 8 Nov 2004, bogdan romocea wrote:> Dear R users, > > I have a data frame which I create with read.csv and then order by > date: > d <- na.omit(read.csv(...)) > d <- d[order(as.Date(as.character(d$Date), format="%d-%b-%y"), > decreasing=F, na.last=F),] > > My problem is that even though the data frame is ordered as > requested, the old row numbers are preserved. For example: > > * Before sorting: > > d[1:3,] > Date Amt > 1 5-Nov-04 87.07 > 2 4-Nov-04 85.80 > 3 3-Nov-04 82.90 > > * After sorting: > > d[1:3,] > Date Amt > 500 12-Nov-02 84.23 > 499 13-Nov-02 85.05 > 498 14-Nov-02 84.95 > > Is there a way to update the row numbers as well? It's not that > important, but I find it a bit confusing.I assure you that not to preserve the row *names* would be very confusing indeed. In your example it might well be more usual to have the Date the row names. If you just want to change the contents of the data frame, and keep the row and column names, use d[] <- your rhs Or use row.names(d) <- seq(len=nrow(d)) to reset the row *names*. -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
You wrote:> Is there a way to update the row numbers as well? It's not that > important, but I find it a bit confusing.They're not actually row numbers, they're row ***names***. These default to row numbers. If they were real-live names you'd want them to be carried along in the sort. To re-set them to be what you want: rownames(d) <- 1:nrow(d) cheers, Rolf Turner rolf at math.unb.ca