5

My data:

        Q5         Q6          Q7
1   Not Agree   Neutral     Not Agree
2   Not Agree   Neutral     Neutral
3   Not Agree   Agree       Agree
4   Not Agree   Agree       Neutral
5   Neutral     Not Agree   Neutral
6   Not Agree   Agree       Neutral
7   Not Agree   Neutral     Neutral
8   Neutral     Agree       Neutral
9   Agree       Neutral     Not Agree
10  Neutral     Agree       Neutral
Q567[1:3] <- lapply(Q567[1:3], factor, levels= c("Agree", "Neutral", "Not Agree"), ordered = TRUE)

likert(Q567) %>%
  plot(type = "bar")

What my data looks like
What my data looks like

I converted them into factor with levels, why I still get the error

Error in likert(Q567) : All items (columns) must have the same number of levels
Martin Brisiak
  • 3,872
  • 12
  • 37
  • 51
Grace Li
  • 51
  • 1
  • 4
  • 1
    Welcome to Stack Overflow. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including example data in a plain text format - for example the output from `dput(yourdata)`. We cannot copy/paste data from images, or understand its structure. – neilfws Jan 11 '21 at 22:24

2 Answers2

15

I've had the same problem, and found that the set I was working with was a tibble, rather than a data.frame:

data <- as.data.frame(data) 

fixed it for me.

Sven Eberth
  • 3,057
  • 12
  • 24
  • 29
Steve
  • 151
  • 1
  • 3
  • 1
    This is a huge time-saver and should be marked as the answer. Will be the first thing I try for future issues, too. – Resource May 01 '22 at 21:20
0

You have only recoded the first three columns of your dataframe to be factors, but you pass the entire dataframe to likert. The likert function expects the variables in Q567 to be factors. So I believe you have additional columns in the dataframe that are not, which is causing your error.

You should do something like:

likert(Q567[,1:3]) %>% 
  plot(type = 'bar')
LMc
  • 12,577
  • 3
  • 31
  • 43
  • It still shows as "Error in likert(Q567[, 1:3]) : All items (columns) must have the same number of levels" – Grace Li Jan 14 '21 at 20:10
  • Did you run `Q567[1:3] <- lapply(Q567[1:3], factor, levels= c("Agree", "Neutral", "Not Agree"), ordered = TRUE)` first before trying the above? – LMc Jan 14 '21 at 20:47
  • Yes I did, it looks like have levels when i do the summary(data). I don't understand why still error – Grace Li Jan 15 '21 at 23:33
  • Can you post a snippet of the data with `dput(head(Q567))`? – LMc Jan 16 '21 at 19:49