[R] Change the names of a dataframe

Duncan Murdoch murdoch.duncan at gmail.com
Tue May 3 22:54:28 CEST 2011


On 03/05/2011 4:43 PM, Jun Shen wrote:
> 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.
>

The first method says:

1.  Get dose[2].  This is a dataframe consisting of the second column of 
dose.  Store it in a temporary, unnamed variable.

2.  Changes its names to 'AMT'. This changes the names of the temporary 
variable, which then disappears.

The second method says:

1. Set the second element of the names associated with dose to 'AMT'.

That's what you want to do.

Duncan Murdoch



More information about the R-help mailing list