I am to perform lm() on different dataset via loop, which should first make dataset using filter() and then to perform lm() on filtered dataset and save the results. However, I always get this error and I have no idea what I am doing wrong.
"Error in
contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels"
My code:
a<-c("var1", "var2", "var3") #character vector
for (b in a){
dataset <- dataset%>%filter(variable_name == b)
lm<- lm(y ~ x1 + x2, data=dataset)
assign(paste(b, "lm", sep='_'), lm)
}
Can anyone help me please? I know it is really elementary. Based on searching results, I've also tried it with
dataset <- dataset%>%filter(variable_name == **paste0("'",b,"'")**)
but always get the same error...Thanks!