Christopher W. Ryan wrote:> R 2.5.1 on WinXP > > I'm trying to create new variables in a dataframe called jaw, as powers > of jaw$age up to the sixth power, and name them > > jaw$age.(--the digit corresponding to the power--) > > Obviously none of these work (silly for me to try, I suppose): > > for (i in 2:6) { > jaw$age.'i' <- jaw$age^i > } > > for (i in 2:6) { > jaw$age.i <- jaw$age^i > } > > for (i in 2:6) { > jaw$age."i" <- jaw$age^i > } > > What is the proper syntax to create and name these variables all at once? > > Thanks. > > --Chrisfor (i in 2:6) jaw[[paste("age", i, sep="^")]] = jaw$age^i or m = outer(jaw$age, 2:6, "^") colnames(m) = paste("age", 2:6, sep="^") jaw = cbind(jaw, m) Best wishes Wolfgang -- ---------------------------------------------------- Wolfgang Huber, EMBL-EBI, http://www.ebi.ac.uk/huber
May be this helps:
jaw<-data.frame(vara=1:20,age=runif(20))
for (i in 2:6) {
jaw[paste("agepow",i, sep="")] <- jaw["age"]^i
}
jaw<-round(jaw,2) # It is just to help the check task
Cheers,
Miltinho Astronauta
Brazil
On 7/14/08, Christopher W. Ryan <cryan@binghamton.edu>
wrote:>
> R 2.5.1 on WinXP
>
> I'm trying to create new variables in a dataframe called jaw, as powers
> of jaw$age up to the sixth power, and name them
>
> jaw$age.(--the digit corresponding to the power--)
>
> Obviously none of these work (silly for me to try, I suppose):
>
> for (i in 2:6) {
> jaw$age.'i' <- jaw$age^i
> }
>
> for (i in 2:6) {
> jaw$age.i <- jaw$age^i
> }
>
> for (i in 2:6) {
> jaw$age."i" <- jaw$age^i
> }
>
> What is the proper syntax to create and name these variables all at once?
>
> Thanks.
>
> --Chris
> --
> Christopher W. Ryan, MD
> SUNY Upstate Medical University Clinical Campus at Binghamton
> 40 Arch Street, Johnson City, NY 13790
> cryanatbinghamtondotedu
> PGP public keys available at http://home.stny.rr.com/ryancw/
>
> "If you want to build a ship, don't drum up the men to gather
wood,
> divide the work and give orders. Instead, teach them to yearn for the
> vast and endless sea." [Antoine de St. Exupery]
>
> ______________________________________________
> 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]]
R 2.5.1 on WinXP
I'm trying to create new variables in a dataframe called jaw, as powers
of jaw$age up to the sixth power, and name them
jaw$age.(--the digit corresponding to the power--)
Obviously none of these work (silly for me to try, I suppose):
for (i in 2:6) {
jaw$age.'i' <- jaw$age^i
}
for (i in 2:6) {
jaw$age.i <- jaw$age^i
}
for (i in 2:6) {
jaw$age."i" <- jaw$age^i
}
What is the proper syntax to create and name these variables all at once?
Thanks.
--Chris
--
Christopher W. Ryan, MD
SUNY Upstate Medical University Clinical Campus at Binghamton
40 Arch Street, Johnson City, NY 13790
cryanatbinghamtondotedu
PGP public keys available at http://home.stny.rr.com/ryancw/
"If you want to build a ship, don't drum up the men to gather wood,
divide the work and give orders. Instead, teach them to yearn for the
vast and endless sea." [Antoine de St. Exupery]
Possibly Parallel Threads
- stuck on making a line graph across time, with 4 categories
- using Sweave with a master file that has several iputted .tex files
- trying to figure out reorder.factor in gdata
- trouble converting an array to a dataframe
- extracting items from R objects and using them in \Sexpr with Sweave--problem with $