Re: [R] how to change the dataframe labels' ?

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Wed 24 Jan 2007 - 13:11:39 GMT

Sorry, no: row.names<-() is what you want.

rownames for matrices (and arrays)
row.names for data frames.

Using them the other way round usually works but can be very inefficient.
>From R-devel (where the worst inefficiencies are circumvented)

      The extractor functions try to do something sensible for any
      matrix-like object 'x'.  If the object has 'dimnames' the first
      component is used as the row names, and the second component (if
      any) is used for the column names.  For a data frame, 'rownames'
      and 'colnames' are calls to 'row.names' and 'names' respectively,
      but the latter are preferred.


On Wed, 24 Jan 2007, talepanda wrote:

> rownames()<- is what you want.
>
>> dat<-data.frame(V1=sample(10),V2=sample(10))
>> dat
> V1 V2
> 1 2 5
> 2 3 8
> 3 8 4
> 4 9 6
> 5 6 2
> 6 5 7
> 7 10 3
> 8 4 9
> 9 1 10
> 10 7 1
>> dat<-dat[order(dat$V2),]
>> dat
> V1 V2
> 10 7 1
> 5 6 2
> 7 10 3
> 3 8 4
> 1 2 5
> 4 9 6
> 6 5 7
> 2 3 8
> 8 4 9
> 9 1 10
>> rownames(dat)<-1:dim(dat)[1]
> ## or rownames(dat)<-dat$V2
>> dat
> V1 V2
> 1 7 1
> 2 6 2
> 3 10 3
> 4 8 4
> 5 2 5
> 6 9 6
> 7 5 7
> 8 3 8
> 9 4 9
> 10 1 10
>
> HTH
>
> On 1/24/07, domenico pestalozzi <statadat@gmail.com> wrote:
>> I import a dataframe composed by 2 variables and 14000 cases. Now I need the
>> labels of the cases sorted by the second variable V2, but if I sort the
>> dataframe according to the second variable:
>>
>> mydataframe<- mydataframe[order(mydataframe$V2),]
>>
>> I notice that the labels are always the same (that is, not ordered by V2).
>>
>> How to change them?
>> I tried :
>>
>> labels(mydataframe)<-1:14000
>>
>> but it doesn't work.
>>
>> Thanks
>>
>> domenico
>>
>> [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> 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
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
>

-- 
Brian D. Ripley,                  ripley@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

______________________________________________
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
and provide commented, minimal, self-contained, reproducible code.
Received on Thu Jan 25 00:16:14 2007

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 25 Jan 2007 - 04:30:33 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.