Skip to content

Scaling part of a data frame

2 messages · Robert Schick, Brian Ripley

#
I have a 30 x 27 data frame, which I'm trying to scale and transform.
Only I want to scale certain all variables except one (the dependent,
which I want to log+1 transform). I can use split() and then scale() and
log1p(), but I'm wondering if I can do this in one call.

I tried apply(), but I could only get the whole data frame, not a part
of it:
Error in apply(chin.sub, chin.sub$density, log1p) : 
        subscript out of bounds

I've tried using tapply:
Error in tapply(chin.sub, chin.sub$density, log1p) : 
        arguments must have same length

Any advice on how to accomplish this?

On a related note how could I go about running the data frame
iteratively through a series of lm() statements without cutting and
pasting similar commands with diff variables or hardcoding the column
names?

e.g. do lm(x~y1)
	lm(x~y2)
	...
     end

Thanks in advance, Rob
platform i386-pc-mingw32
arch     i386           
os       mingw32        
system   i386, mingw32  
status                  
major    1              
minor    6.0            
year     2002           
month    10             
day      01             
language R
#
On Thu, 14 Nov 2002, Robert Schick wrote:

            
you want something like

test <- chin.sub
m <- match("density", names(test)
test[-m] <- scale(test[-m])
test$density <- log1p(test$density)
Depends what you want to do with the results. E.g. add1 will add all
single variables and report the reduction in SSq etc.  Or something like

m <- names(DF)
m <- m[-match("x", m]
do.call(lm, lapply(paste("x ~", m), as.formula))

[untested]