Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何替换公式中的因变量?_String_R_Formula - Fatal编程技术网

String 如何替换公式中的因变量?

String 如何替换公式中的因变量?,string,r,formula,String,R,Formula,假设我有一个R公式,这样: fm <- formula(y~x1+x2+x1:x2) 那么: fm <- formula(y~x1+x2+x1:x2) for (newy in c("y1","y2","y3")){ newfm <- reformulate(deparse(fm[[3]]),response=newy) print(newfm) } ## y1 ~ x1 + x2 + x1:x2 ## y2 ~ x1 + x2 + x1:x2 ## y3 ~

假设我有一个R公式,这样:

fm <- formula(y~x1+x2+x1:x2)
那么:

fm <- formula(y~x1+x2+x1:x2)
for (newy in c("y1","y2","y3")){
    newfm <- reformulate(deparse(fm[[3]]),response=newy)
    print(newfm)
}
## y1 ~ x1 + x2 + x1:x2
## y2 ~ x1 + x2 + x1:x2
## y3 ~ x1 + x2 + x1:x2
工作,因为R将在进入for循环之前尝试计算
c(y1,y2,y3)

或者尝试以下操作:

for (i in 1:3) {
    as.formula(paste0("y",i,"~x1+x2+x1:x2"))
}

你能用这个例子给出答案吗?你能先看一看,看看它如何帮助你解决你自己的问题吗?我感到沮丧的是原始公式中的交互项,以及如何将其索引到
重新格式化
参数。看看
术语(fm)
来获得指示这是一项伟大的工作,本+1为什么您需要
stringi::stri_paste
?base R的
paste0
有什么问题?
for (newy in c(y1,y2,y3)) {
    ...
}
for (i in 1:3) {
    as.formula(paste0("y",i,"~x1+x2+x1:x2"))
}