I need help pasting a data frame column as a formula in R using the $ sign.
df1 <- data.frame(id1 = c(1, 2, 3, 4, 5),
id2 = c(1, 2, 3, 4, 5),
var1 = c(3,6,6,9,5),
var2 = c(1, 1, 0, 0, 1))
rownames(df1)<-c("first","second","third","fourth","fifth")
trait=c("id1","id2")
list=as.character("var1","var2")
for(i in trait){
fit <- lm(df1[,i]~df1$list[2])
}
I want to paste the second part of the lm (df1$list[2]). After the $, I want that it pastes the corresponding element of the character vector list.