In the first case you create a new data frame consisting of the 2nd column of
the original, then change the name of the only column in that new data frame,
then since nothing is done with that data frame it gets thrown away. So it is
not that nothing happened, but just that nothing useful happened. The original
data frame was never changed, just a copy of it.
The second method access the names, then changes the 2nd name and therefore
changes the actual data frame of interest.
--
Gregory (Greg) L. Snow Ph.D.
Statistical Data Center
Intermountain Healthcare
greg.snow at imail.org
801.408.8111
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Jun Shen
> Sent: Tuesday, May 03, 2011 2:43 PM
> To: R-help
> Subject: [R] Change the names of a dataframe
>
> Dear list,
>
> This may sound silly. What is the right way to change the names of a
> dataframe? Let's say I have this data frame (dose) with four columns
> with
> names "ID", "DOSE", "TIME" "CMT". I
want to change "DOSE" to "AMT". So
> I did
>
> names(dose[2])<-'AMT'
>
> But nothing happened. The name of the second column is still
"DOSE".
>
> Only this works
>
> names(dose)[2]<-'AMT'
>
> I wonder what is wrong with the first method. Thanks.
>
> Jun
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org 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.