如何用两个替换R公式中的一个? [英] How can I replace one term in an R formula with two?

查看:81
本文介绍了如何用两个替换R公式中的一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些类似

y ~ x + z

我想将其转换为

y ~ x_part1 + x_part2 + z

更一般地说,我想有一个函数,该函数接受一个公式并返回该公式,并将所有与"^ x $"匹配的术语替换为"x_part1"和"x_part2".这是我当前的解决方案,但是感觉很不舒服...

More generally, I would like to have a function that takes a formula and returns that formula with all terms that match "^x$" replaced by "x_part1" and "x_part2". Here's my current solution, but it just feels so kludgey...

my.formula <- fruit ~ apple + banana
var.to.replace <- 'apple'
my.terms <- labels(terms(my.formula))
new.terms <- paste0('(', 
                    paste0(var.to.replace, 
                           c('_part1', '_part2'),
                           collapse = '+'),
                    ')')
new.formula <- reformulate(termlabels = gsub(pattern = var.to.replace,
                                             replacement = new.terms,
                                             x = my.terms),                                 
                           response = my.formula[[2]])

另一个需要注意的是,可以通过交互来指定输入公式.

An additional caveat is that the input formula may be specified with interactions.

y ~ b*x + z

应输出以下(等效)公式之一

should output one of these (equivalent) formulae

y ~ b*(x_part1 + x_part2) + z
y ~ b + (x_part1 + x_part2) + b:(x_part1 + x_part2) + z
y ~ b + x_part1 + x_part2 + b:x_part1 + b:x_part2 + z

MrFlick提倡使用

MrFlick has advocated the use of

替换(y〜b * x + z,list(x = quote(x_part1 + x_part2)))

substitute(y ~ b*x + z, list(x=quote(x_part1 + x_part2)))

但是当我将公式存储后,我想在变量中进行修改,例如

but when I have stored the formula I want to modify in a variable, as in

my.formula <- fruit ~ x + banana

这种方法似乎需要更多按摩:

This approach seems to require a little more massaging:

substitute(my.formula, list(x=quote(apple_part1 + apple_part2)))
# my.formula

对该方法的必要更改是:

The necessary change to that approach was:

do.call(what = 'substitute',
        args = list(apple, list(x=quote(x_part1 + x_part2))))

但是当'x'和c('x_part','x_part2')都存储在具有名称的变量中时,我不知道如何使用这种方法.上面的var.to.replacenew.terms.

But I can't figure out how to use this approach when both 'x' and c('x_part', 'x_part2') are stored in variables with names, e.g. var.to.replace and new.terms above.

推荐答案

如何将公式作为字符串使用?许多基本的R模型,例如lm()都接受字符串公式(否则,您始终可以使用formula()).在这种情况下,您可以使用gsub():

How about working with the formula as a string? Many base R models like lm() accept a string formulas (and you can always use formula() otherwise). In this case, you can use something like gsub():

f1 <- "y ~ x + z"
f2 <- "y ~ b*x + z"

gsub("x", "(x_part1 + x_part2)", f1)
#> [1] "y ~ (x_part1 + x_part2) + z"

gsub("x", "(x_part1 + x_part2)", f2)
#> [1] "y ~ b*(x_part1 + x_part2) + z"

例如,使用mtcars数据集,并说我们要用disp + hp(x_part1 + x_part2)替换mpg(x):

For example, with mtcars data set, and say we want to replace mpg (x) with disp + hp (x_part1 + x_part2):

f1 <- "qsec ~ mpg + cyl"
f2 <- "qsec ~ wt*mpg + cyl"

f1 <- gsub("mpg", "(disp + hp)", f1)
f2 <- gsub("mpg", "(disp + hp)", f2)

lm(f1, data = mtcars)
#> 
#> Call:
#> lm(formula = f1, data = mtcars)
#> 
#> Coefficients:
#> (Intercept)         disp           hp          cyl  
#>    22.04376      0.01017     -0.02074     -0.56571

lm(f2, data = mtcars)
#> 
#> Call:
#> lm(formula = f2, data = mtcars)
#> 
#> Coefficients:
#> (Intercept)           wt         disp           hp          cyl  
#>   20.421318     1.554904     0.026837    -0.056141    -0.876182  
#>     wt:disp        wt:hp  
#>   -0.006895     0.011126

这篇关于如何用两个替换R公式中的一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆