Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

creating a loop for pirate plots

I have a problem creating a loop for pirateplots. My dataframe contains several columns with values 0 or 1 and a column with some specific score and looks like this:

Q1  Q2  Q3  ... Score
1   0   0       145
0   1   1       167
...
 

I already created a pirate plot for one column that works:

pirateplot(formula = score ~ Q1,
           data = df,      
           main = "Question",
           xlab = "answer",
           ylab = "score")

Now I would like to create a loop to create a plot for each of the 28 columns. Here is what I tried:

loop.vector <- 1:28

for (i in loop.vector) { 
  x <- df[,i]

         print(pirateplot(formula = score~ i,
                          data = df,
                          xlab = "answer",
                          ylab = "score",
                    main = paste("Question", i)
         ))
}

This is the error I get:

Error in model.frame.default(formula = formula, data = data) : Variablenlängen sind unterschiedlich (gefunden für 'i') (meaning: different variable length)

Please help!

Comments