[R] Scaling part of a data frame

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Fri Nov 15 08:40:11 CET 2002

On Thu, 14 Nov 2002, Robert Schick wrote:

> 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:

you want something like

test <- chin.sub
m <- match("density", names(test)
test[-m] <- scale(test[-m])
test$density <- log1p(test$density)

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

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


Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

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