Hi Esmail,

Try this:

# A simple example
set.seed(123)
X=matrix(rnorm(100*5),ncol=5)
colnames(X)=c("Y",paste("X",1:4,sep=""))
X[1:5,]
Y         X1         X2         X3          X4
[1,] -0.56047565 -0.7104066  2.1988103 -0.7152422 -0.07355602
[2,] -0.23017749  0.2568837  1.3124130 -0.7526890 -1.16865142
[3,]  1.55870831 -0.2466919 -0.2651451 -0.9385387 -0.63474826
[4,]  0.07050839 -0.3475426  0.5431941 -1.0525133 -0.02884155
[5,]  0.12928774 -0.9516186 -0.4143399 -0.4371595  0.67069597

# Square values
X2=X[,2:5]^2

# Result
res=cbind(X,X2)
colnames(res)=c(colnames(X),paste(colnames(X)[-1],"^2",sep=""))
res[1:5,]
               Y         X1         X2         X3          X4
X1^2      X2^2      X3^2         X4^2
[1,] -0.56047565 -0.7104066  2.1988103 -0.7152422 -0.07355602 0.50467749
4.8347670 0.5115714 0.0054104880
[2,] -0.23017749  0.2568837  1.3124130 -0.7526890 -1.16865142 0.06598924
1.7224278 0.5665407 1.3657461518
[3,]  1.55870831 -0.2466919 -0.2651451 -0.9385387 -0.63474826 0.06085688
0.0703019 0.8808549 0.4029053598
[4,]  0.07050839 -0.3475426  0.5431941 -1.0525133 -0.02884155 0.12078586
0.2950598 1.1077842 0.0008318352
[5,]  0.12928774 -0.9516186 -0.4143399 -0.4371595  0.67069597 0.90557790
0.1716776 0.1911085 0.4498330825


HTH,

Jorge


On Tue, May 6, 2008 at 9:43 PM, Esmail Bonakdarian <esmail.js@gmail.com>
wrote:

> 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
>
> ______________________________________________
> R-help@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.
>

	[[alternative HTML version deleted]]

