2016-10-19 41 views
0

我需要一个for循环R.“正常”循环公式里面是R中非常简单的(例如,见这里https://www.r-bloggers.com/how-to-write-the-first-for-loop-in-r/),但因为有“”我的公式中不出现工作......有没有人知道解决这个问题的方法?在这个公式我想ID1是一个变量,而我有500点不同的IDR代表公式中环

testing <- formula(paste("STATUS ~ as.factor(data$TEMP) + C1 + ID1 * ID2 ")) 
+0

你想要所有的ID之间的所有双向交互?如果是的话,你可以修改[this](http://stackoverflow.com/questions/11633403/how-to-automatically-include-all-2-way-interactions-in-a-glm-model-in-r)与'update()'函数。 – roman

+0

嗯有趣!是的,我希望那样!你能帮忙一下吗(我不是R专家)那会是什么样子? –

+0

我添加了一个答案,但让我知道如果你需要更多的帮助 – roman

回答

0

使用as.formula而不仅仅是formula列表。这应该在paste上工作。虽然你的版本适用于我..我从formula这两种方式。设置sep="",所以它不包括在公式中的任何更多的空间... 关于第二部分,我不知道你在问什么。你也想要它作为因素?

2
y <- rnorm(10) 
x1 <- rnorm(10) 
x2 <- rnorm(10) 
x3 <- rnorm(10) 
d <- data.frame(y, x1, x2, x3) 

# fit a model with all 2 way interactions 
model1 <- lm(y ~ .*., d) 
# remove one that you don't want 
model2 <- update(model1, ~.-x2:x3)