[R] Automatically generating new column names (and columns)

Esmail Bonakdarian esmail.js at gmail.com
Wed May 7 03:43:09 CEST 2008


Once again I need to tap into the collective knowledge here.

Let's say I have the following columns and data below

Y X1 X2 X3 X4

I would like to generate additional new columns and column names
(ie the data would be squared - and I'd like the column names to
reflect this) like:

Y X1 X2 X3 X4  X1^2 X2^2 X3^2 X4^2


I believe I can compute the values correctly with the code below, but
I am unable to generate the column names. (This is a short example, in
reality I have many more columns and more complicated names)

for(i in 2:5)
{
    name=get(paste(names(simpleData)[i],"^2", sep=""))
    simpleData<- transform(simpleData, name=(simpleData[,i]^2))
}

print(simpleData)

Any suggestions/hints .. I've searched the FAQ and web (this is how I
came across the get() function) but no luck so far.

Thanks,
Esmail



More information about the R-help mailing list