[R] function to transform response of a formula

Paul Emberson email at calidasoft.co.uk
Wed Jul 30 11:50:26 CEST 2008


Hi,

I am trying to write a function which takes a formula as input and 
outputs a new formula with a different response while keeping the rest 
of the formula the same.

I.e.

respapply : ( y ~ a+b ) -> ( f(y) ~ a + b )

I have tried the following but it doesn't work.  The terms become 
invalid as shown below.

respapply <- function(fm, f) {

     fm[[2]] <- f(eval(fm[[2]]))
     fm;

}

 > fm <- formula(y ~ a + b)
 > y <- runif(5)
 > newfm <- respapply(fm,identity)
 > newfm

c(0.552921097259969, 0.939932722365484, 0.62522904924117, 
0.899310540175065,
0.877736972644925) ~ a + b

 > terms(newfm)
Error in terms.formula(fmapply(fm, identity)) :
   invalid term in model formula

Could someone put me in the right direction of how to correctly write a 
respapply function as described.

Regards,

Paul Emberson



More information about the R-help mailing list