[R] just a small variable-naming question

Keith Jewell k.jewell at campden.co.uk
Tue Aug 27 11:11:26 CEST 2013


In case the OP wanted to append columns, rather than rename existing 
columns:

cbind(myData, var1=NA, var2=NA, var3=NA)
   col1 col2 col3 var1 var2 var3
1    1    2    3   NA   NA   NA
2    2    3    4   NA   NA   NA
3    3    4    5   NA   NA   NA


On 24/08/2013 17:22, arun wrote:
> Hi,
> You could use ?paste()
> colnames(myData)<-paste(colnames(myData),paste0("var",1:3),sep="_")
>   myData
> #  col1_var1 col2_var2 col3_var3
> #1         1         2         3
> #2         2         3         4
> #3         3         4         5
>
> A.K.
>
>
>
> ----- Original Message -----
> From: Richard Sherman<rss.pdx at gmail.com>
> To: r-help at r-project.org
> Cc:
> Sent: Saturday, August 24, 2013 12:09 PM
> Subject: [R] just a small variable-naming question
>
> Hi all,
>
> If I have this little data set:
>
>> myData<- data.frame(col1 = 1:3, col2 =2:4, col3 = 3:5)
>> myData
>    col1 col2 col3
> 1    1    2    3
> 2    2    3    4
> 3    3    4    5
>
> and I want to add (not replace) the names var1, var2, var3 to the existing col1, col2, col3, what is the right way to do that?
>
> Thanks.
>
> -Richard
>
> ---
> Prof. Richard Sherman
> Division of International Studies
> Korea University
>
> ______________________________________________
> 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.
>
>



More information about the R-help mailing list