I have the following dataset:
df <- data.frame(row_id = c(100, 101, 102, 103, 104, 105, 106, 107, 108, 109),
level = c(1000,2000,3000,4000,5000,6000,7000,8000,9000,10000),
col1 = c(1,0,1,1,1,0,0,1,1,0),
col2 = c(1,1,1,0,0,1,1,1,0,0),
col3 = c(0,0,1,0,0,1,1,1,1,0),
col4 = c(1,1,1,0,0,1,0,1,1,1),
col5 = c(1,1,1,0,1,0,1,0,0,1))
I would like to do a linear regression on the variable level
with each of the other columns with prefix col
. I would like to use the for loop function to do this instead of doing the following:
lm1<-lm(level~col1, data=df)
lm2<-lm(level~col2, data=df)
lm3<-lm(level~col3, data=df)
lm4<-lm(level~col4, data=df)
lm5<-lm(level~col5, data=df)
Any help would be much appreciated, thanks!