Sören Vogel
2017-Mar-28 14:14 UTC
[R] How to apply calculations in "formula" to "data frame"
Hello Ho can I apply a formula to a data frame? 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
peter dalgaard
2017-Mar-28 14:57 UTC
[R] How to apply calculations in "formula" to "data frame"
> 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
Sören Vogel
2017-Mar-28 18:00 UTC
[R] How to apply calculations in "formula" to "data frame"
`model.matrix` was what I was looking for. Thanks, S?ren> On 28.03.2017, at 16:57, peter dalgaard <pdalgd at gmail.com> wrote: > > >> 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 > > > > > > > > >