简体   繁体   English

带有ifelse()语句的lm()-R

[英]lm() with ifelse() statement - R

Let's assume I have this dataframe: 假设我有这个数据框:

df <- data.frame(GN1 = sample(1:10, 10 ,replace=TRUE),
           GN2 = sample(1:10, 10 ,replace=TRUE),
           GN3 = sample(1:10, 10 ,replace=TRUE),
           E10 = sample(1:10, 10 ,replace=TRUE),
           PSV7 = sample(1:10, 10 ,replace=TRUE),
           PEC3 = sample(1:10, 10 ,replace=TRUE),
           PEC4 = sample(1:10, 10 ,replace=TRUE),
           AC6 = sample(1:10, 10 ,replace=TRUE),
           AC7 = sample(1:10, 10 ,replace=TRUE),
           stringsAsFactors = FALSE)

   GN1 GN2 GN3 E10 PSV7 PEC3 PEC4 AC6 AC7
1    7   3  10   6    4    4    3   9   3
2    2   5   6   6    6    6    5   7   1
3    7   6  10   6    9    1    9   7   5
4    7   1   8   9    2    4    5   5   7
5    8   3   3   8    6    8    9   5  10
6    7   1   1   8    9    3    8   9   4
7    4   6   4   7    2    6    9   8   9
8    7   8   8   7    2    1    7   6   5
9    1   9   4   8    5    5    2   7   1
10   4   9   2   1    4    4   10   2   9

And I want to run this formula: 我想运行以下公式:

c_SA=lm(formula = GN1 ~ ifelse(df2$if_a == 1,PEC3+PEC4+AC6,GN2+GN3+E10+PSV7+PEC3), data = df)

Where df2$if_a is a external value from df and it just can take the values 0 or 1 ( df2 has just one row). 其中df2$if_adf的外部值,它只能取值01df2只有一行)。 As you can see above, If df2$if_a == 1 I need to run the first "pack" of variables, while if its equal to 0, I need to run the other "pack" of variables. 从上面可以看到,如果df2$if_a == 1我需要运行变量的第一个“包”,而如果它等于0,则需要运行变量的另一个“包”。

I have tried as.formula() and reformulate() without success: 我尝试过as.formula()as.formula() reformulate()没有成功:

c_SA=lm(formula = GN1 ~ ifelse(df2$if_a == 1,as.formula(PEC3+PEC4+AC6),as.formula(GN2+GN3+E10+PSV7+PEC3)), data = df)

Also, there are some similar questions ( 1 , 2 , 3 ). 此外,还有一些类似的问题( 123 )。 However, they subset the dataframe in the data = argument, and I need to make the formula = argument subject to a value of a external source. 但是,它们将data =参数中的data =框作为子集,而我需要使formula =参数服从外部源的值。

Any suggestions? 有什么建议么?

Use if (not vectorized ifelse ---since you're ((hopefully)) not using a vector) to select the formula you want, rather than trying to use it inside the formula: 使用if (不是向量化ifelse ---因为您是((希望))不使用向量)选择想要的公式,而不是尝试在公式中使用它:

my_formula = if (df2$if_a == 1) {
  GN1 ~ PEC3 + PEC4 + AC6
} else {
 GN1 ~ GN2 + GN3 + E10 + PSV7 + PEC3
}

c_SA = lm(formula = my_formula, data = df)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM