Re: [R] transform() on selective names. Is it possible?

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Thu, 07 Apr 2011 10:12:23 -0400

On Apr 7, 2011, at 9:56 AM, Juan Carlos Borrás wrote:

> Hi all,
> I am whitening my data:
>
> # code begins
> N <- 300
> M <- 2
> x <- matrix(data=rnorm(N*M, 0, 3)-10, ncol=M, nrow=N)
> y <- matrix(c(1,-2,-2,1), ncol=M, nrow=M)
> z <- data.frame(x %*% y)
> colnames(z) <- c('x','y')
> par(mfrow=c(1,3))
> plot(z, pch=5, col="blue")
>
> whiten <- function(x) { (x-mean(x))/sd(x) }

Consider:

  whiten <- scale # no need to re-invent the wheel   fc <- function(dfrm, coln) transform(dfrm, coln=whiten(dfrm[coln]))   colxy <- "x"
  z <- fc(z, colxy) # the "[" function will interpret colxy   z

>
> zz <- transform(z, x=whiten(x), y=whiten(y))
> plot(zz, pch=3, col="red")
>
> #code ends
>
> And everything looks fine enough.
> But now I want to withen just one of the columns and I won't know
> which one until my script is running, hence I can't hard code it in
> the script.
> Then I though, well maybe if I define some convenient f...
>
> #begin code
>
> f <- function(a) { paste(a,"=withen(",a,")", sep='') }
> a <- 'x' # or a <- 'y' depending on user input.....
> f(a)
>> [1] "x=withen(x)"
> # so I could try....
> zzz <- transform(z, eval(f('x')))
> # which of course doesn't work
> plot(zz, pch=3, col="green")
>
> head(z, n=2)
>> x y
>> 1 17.167380 6.884402
>> 2 8.234507 13.940932
> head(zzz, n=2)
>> x y
>> 1 17.167380 6.884402
>> 2 8.234507 13.940932
>
> #end code
>
> Could someone provide me with some hint on whether the attempted trick
> above is possible and how to proceed further?
> Thanks in advance.
> jcb!
>
> ______________________________________________
> 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.

David Winsemius, MD
West Hartford, CT



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 Thu 07 Apr 2011 - 14:14:47 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 Thu 07 Apr 2011 - 14:50:27 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