Olympia
Olympia

Reputation: 537

Different variable lengths when looping over a string vector which corresponds to data frame columns

I am new in writing loops and I have some difficulties there. I already looked through other questions, but didn't find the answer to my specific problem.

So lets just create a random dataset, give column names and set the variables as character:

d<-data.frame(replicate(4,sample(1:9,197,rep=TRUE)))
colnames(d)<-c("variable1","variable2","trait1","trait2")
d$variable1<-as.character(d$variable1)
d$variable2<-as.character(d$variable2)

Now I define my vector over which I want to loop. It correspons to trait 1 and trait 2:

trt.nm <- names(d[c(3,4)]) 

Now I want to apply the following model for trait 1 and trait 2 (which should now be as column names in trt.nm) in a loop:

library(lme4)
for(trait in trt.nm)
{
  lmer (trait ~ 1 + variable1 + (1|variable2) ,data=d)
}

Now I get the error that variable lengths differ. How could this be explained?

If I apply the model without loop for each trait, I get a result, so the problem has to be somewhere in the loop, I think.

Upvotes: 1

Views: 66

Answers (1)

twedl
twedl

Reputation: 1648

trait is a string, so you'll have to convert it to a formula to work; see http://www.cookbook-r.com/Formulas/Creating_a_formula_from_a_string/ for more info.

Try this (you'll have to add a print statement or save the result to actually see what it does, but this will run without errors):

for(trait in trt.nm) {
  lmer(as.formula(paste(trait, " ~ 1 + variable1 + (1|variable2)")), data = d)
}

Another suggestion would be to use a list and lapply or purrr::map instead. Good luck!

Upvotes: 1

Related Questions