Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using function arguments in update.formula

Tags:

r

I am writing a function that takes two variables and separately regresses each of them on a set of controls expressed as a one-sided formula. Right now I'm using the following to make the formula for one of the regressions, but it feels a bit hacked-up:

foo <- function(x, y, controls) {
    cl <- match.call()
    xn <- cl[["x"]]
    xf <- as.formula(paste(xn, deparse(controls)))
}

I'd prefer to do this using update.formula(), but of course update.formula(controls, x ~ .) and update.formula(controls, as.name(x) ~ .) don't work. What should I be doing?

like image 382
brentonk Avatar asked Jul 31 '09 17:07

brentonk


People also ask

Can we include a function while creating a formula?

You can create a formula to calculate values in your worksheet by using a function. For example, the formulas =SUM(A1:A2) and SUM(A1,A2) both use the SUM function to add the values in cells A1 and A2.

How do I change the argument in an Excel formula?

1) Using the Insert Function button to display the Function Arguments dialog box. 2) Using the shortcut key (Shift + F3) to display the Function Arguments dialog box. 3) Editing the formulas manually using the Formula Bar. 4) Editing the formulas manually using the shortcut key (F2).

What's the difference between parameter and argument?

Argument Vs. Parameter : Explore the Major Difference between Argument and Parameter. The values that are declared within a function when the function is called are known as an argument. Whereas, the variables that are defined when the function is declared are known as a parameter.


1 Answers

Here's one approach:

right <- ~ a + b + c
left <- ~ y 
left_2 <- substitute(left ~ ., list(left = left[[2]]))

update(right, left_2)

But I think you'll have to either paste text strings together, or use substitute. To the best of my knowledge, there are no functions to create one two sided formula from two one-sided formulas (or similar equivalents).

like image 120
hadley Avatar answered Sep 27 '22 21:09

hadley