[R] apply question

John Fox jfox at mcmaster.ca
Wed Mar 6 02:50:58 CET 2002


Dear Michael,

At 05:19 PM 3/5/2002 -0500, Michaell Taylor wrote:

>Sorry to bother the list with another apply question, but I am having
>trouble getting my mind around the various apply forms.  A simplified
>version of my problem is the following:
>
>data _
>data.frame(ct=seq(1,10),a1=rnorm(10),a2=rnorm(10),a3=rnorm(10),b=rnorm(10))
>listofAvars _ grep('^a',names(data),value=T)
>
>I would like a1, a2, a3 to be multiplied by 10. That is, I would like a
>cleaner way of specifying the following:
>
>a1 <- a1*10
>a2 <- a2*10
>a3 <- a3*10
>
>As I mentioned, this is a simplified version of a larger variables list
>and slightly more complicated transformation.

If I understand correctly what you want to do, then you don't need apply. Try

data[, listofAvars] <- 10*data[, listofAvars]

This doesn't produce new versions of a1, a2, and a3 in the global 
environment but it does transform the variables in the data frame, which 
could be subsequently attached. If you don't want to alter the original 
data, then you could make a copy of the data frame first.

Does that help?

John



-----------------------------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario, Canada L8S 4M4
email: jfox at mcmaster.ca
phone: 905-525-9140x23604
web: www.socsci.mcmaster.ca/jfox
-----------------------------------------------------

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list