[R] misleading output after ordering data frame
Thomas Lumley
tlumley at u.washington.edu
Mon Nov 8 18:27:24 CET 2004
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
More information about the R-help
mailing list