[R] question about a small "for" loop

From: Christopher W. Ryan <cryan_at_binghamton.edu>
Date: Mon, 14 Jul 2008 17:16:56 -0500


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_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.
Received on Mon 14 Jul 2008 - 21:26:43 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 14 Jul 2008 - 22:31:48 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive