[R] How to apply calculations in "formula" to "data frame"

peter dalgaard pdalgd at gmail.com
Tue Mar 28 16:57:38 CEST 2017


> On 28 Mar 2017, at 16:14 , Sören Vogel <soeren.vogel at posteo.ch> wrote:
> 
> Hello
> 
> Ho can I apply a formula to a data frame?


That would depend on whether the formula has any special interpretation.

If if is just an elementary expression, then it would be like 

eval(For1[[3]], Data, environment(For1))

but you are using "." to represent... what exactly?

One possibility is model.matrix(For1, Data)

but I'm not at all sure that that is what you want.

-pd

> 
> library("formula.tools")
> Data <- data.frame("v1" = rnorm(31), "v2" = runif(31), "v3" = sample(1:7, 31, repl=T), "v4" = rlnorm(31))
> For1 <- as.formula(v1 ~ .^3)
> Lhs <- Data[, formula.tools::lhs.vars(formula)]
> Rhs <- apply_formula_to_data_frame_and_return_result(data, formula) # ???
> 
> Thank you,
> Sören
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Office: A 4.23
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list