Skip to content

transform() on selective names. Is it possible?

5 messages · Juan Carlos Borrás, David Winsemius

#
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) }

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)
# 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)
head(zzz, n=2)
#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!
#
On Apr 7, 2011, at 9:56 AM, Juan Carlos Borr?s wrote:

            
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
David Winsemius, MD
West Hartford, CT
#
Wonderful, or the closest to heaven I've been the whole afternoon, but
not quite there:

# begin code
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 <- scale  # no need to re-invent the wheel, I agree
fc <- function(dfrm, coln) transform(dfrm, coln=whiten(dfrm[coln]))
colxy <- "x"
z1 <- fc(z, colxy)  # the "[" function will interpret colxy
colnames(z1)
fc <- function(dfrm, coln) transform(dfrm, coln=whiten(dfrm[,coln]))
colxy <- "x"
z2 <- fc(z, colxy)  # the "[" function will interpret colxy
colnames(z2)
#end code

What I want is to know whether I can customize the column name of the
result of the transform() call.
Your hint is fantastic, thanks there, but I keep getting into that
particular pattern of computation over and over and I wonder if it's
possible to skip a column clean-up after applying your trick.

2011/4/7 David Winsemius <dwinsemius at comcast.net>:
Cheers,
jcb!
#
On Apr 7, 2011, at 10:27 AM, Juan Carlos Borr?s wrote:

            
I haven't stumbled on a solution to that task. I am wondering if you  
could use something like:

inpnames <- names(dfrm) # before the transform step
outdfrm <- transform(...)
names(outdfrm) <- c(inpnames, paste(colxy, "new", sep="_") )

.... kludgy to be sure.
David Winsemius, MD
West Hartford, CT
#
# The code demonstrating the final version I am going to use is as follows

rm(list=ls()) # Beware of this one so it doesn't spoil your workspace

N <- 100
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,4))
plot(z, pch=3, col="black")

withen <- function(dfrm, coln) {
  x <- transform(dfrm, coln=scale(dfrm[,coln]))
  if (length(coln)==1) {
    y <- x
    y[,coln] <- NULL
    colnames(y) <- sub("^coln",coln,colnames(y))
  } else {
    y <- x[,grep("^coln", colnames(x))]
    colnames(y) <- sub("^coln.","",colnames(y))
  }
  return(y)
}

colx <- c("x")
coly <- c("y")
colxy <- c("x", "y")

z1 <- withen(z, colx)  # the "[" function will interpret colxy
plot(z1, pch=5, col="magenta")

z2 <- withen(z, colxy)  # the "[" function will interpret colxy
plot(z2, pch=7, col="green")

z3 <- withen(z, colxy)  # the "[" function will interpret colxy
plot(z3, pch=9, col="blue")

#Cheers,
#jcb!

2011/4/7 David Winsemius <dwinsemius at comcast.net>: