On Nov 21, 2012, at 4:35 PM, nprause wrote: > I would like to add an extension to the current name of a variable to create > a new variable that is its sqrt transform. Each piece of the equation below > works independently, but the left side definition fails on run. I also tried > creating the variable name first, but ended up with an object that > toString() did not fix. Better ideas? > > Example 1 > (paste0(emotions[[i]],"_sqrt"))<-sqrt(data[[emotions[i]]])
Easiest way would be to use "[[<-" (since there is no 'paste0<-' function: data[[ paste0(emotions[[i]],"_sqrt") ] <- sqrt(data[[ emotions[i] ]]) Now I'm pretty sure that would not work as it stands if the length of "i" were greater than 1, but if "i" were a single number or character value being delivered inside a loop or equivalently inside sapply(), it should work. You can also look at: ?assign > > Example 2 > newvar<-toString(paste0(emotions[[i]],"_sqrt")) > data$newvar<-sqrt(data[[emotions[i]]]) > > Example 3 > newvar<-toString(paste0(emotions[[i]],"_sqrt")) > data$newvar[1]<-sqrt(data[[emotions[i]]]) > > > > -- > View this message in context: > http://r.789695.n4.nabble.com/Efficiently-creating-defining-new-variables-transformations-tp4650401.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. David Winsemius, MD Alameda, CA, USA ______________________________________________ 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.