[R] How to change all name of variables

Gabor Grothendieck ggrothendieck at gmail.com
Thu Jun 2 15:21:35 CEST 2005


On 6/2/05, Muhammad Subianto <subianto at gmail.com> wrote:
> Dear R-helpers,
> First I apologize if my question is quite simple
> I have a large datasets which more 100 variables.
> For a research I need to change all name of variables with add one or
> more letters on each variables.
> For example,
>  > data(Pima.tr)
>  > Pima.tr[1:5,]
>   npreg glu bp skin  bmi   ped age type
> 1     5  86 68   28 30.2 0.364  24   No
> 2     7 195 70   33 25.1 0.163  55  Yes
> 3     5  77 82   41 35.8 0.156  35   No
> 4     0 165 76   43 47.9 0.259  26   No
> 5     0 107 60   25 26.4 0.133  23   No
>  >
>  > dimnames(Pima.tr)[[2]]
> [1] "npreg" "glu"   "bp"    "skin"  "bmi"   "ped"   "age"   "type"
>  >
> 
> I need to change the variables name ,
> "npreg" "glu" "bp" "skin" "bmi" "ped" "age" "type"
> with
> "xyz.npreg" "xyz.glu" "xyz.bp" "xyz.skin" "xyz.bmi" "xyz.ped" "xyz.age"
> "xyz.type"
> 
> How can I make this (automatically). I don't want to make manual with
> more 100 variables.

Try this:

names(prima) <- paste("xyz", names(prima), sep = ".")




More information about the R-help mailing list