rlikert

Error: length of 'dimnames' [2] not equal to array extent from as.data.frame


I am trying to read data from an excel sheet, into a likert diagram. I have used this code successfully to generate 10's of likert scale data visualizations, but just this morning I am getting the following error (even from old code that worked less than 7 days ago!). I have not altered the original Excel file.

Here is my code (I apologize if this is wrong, I could not locate the insert "R" code option):

library(ggplot2)
library(readxl)
library(tidyverse)
library(HH)

thesis <- read_excel("C:/Users/aarde/Desktop/Thesis_scrubbed.xlsx",sheet = "Gamer Data",range = "K1:M8")

names(thesis) <- c("Q10. More Scenarios","Q18. Play Again?", "Q20. More Code Orange")
thesis$`Q10. More Scenarios` = factor(thesis$`Q10. More Scenarios` , levels = c("1 - Strongly Disagree", "2 - Disagree", "3 - Neutral", "4 - Agree", "5 - Strongly Agree"), ordered = TRUE)
thesis$`Q18. Play Again?` = factor(thesis$`Q18. Play Again?`, levels = c("1 - Strongly Disagree", "2 - Disagree", "3 - Neutral", "4 - Agree", "5 - Strongly Agree"), ordered = TRUE)
thesis$`Q20. More Code Orange` = factor(thesis$`Q20. More Code Orange`, levels = c("1 - Strongly Disagree", "2 - Disagree", "3 - Neutral", "4 - Agree", "5 - Strongly Agree"), ordered = TRUE)
thesis <- as.data.frame(thesis)
#thesis <- subset(thesis, select = -NA)

thesisLikert = likert(thesis)

p <- likert::likert.bar.plot(thesisLikert, group.order = names(thesis), center = 3)
p + labs(title = "Future Expansion", subtitle = "Gamers", x = "Questions", y = "Percentage Response")

Here is what I have done so far:

What I know so far:

enter image description here

I see no discrepancy here - dimnames[2] appears to have 3 elements contained within it. I have 3 elements in my thesis object. Any clarification is appreciated.

edit: small typo

dputs output:

dput(thesis)
structure(list(`Q10. More Scenarios` = structure(c(5L, 3L, 4L, 
5L, 5L, 4L, 4L), levels = c("1 - Strongly Disagree", "2 - Disagree", 
"3 - Neutral", "4 - Agree", "5 - Strongly Agree"), class = c("ordered", 
"factor")), `Q18. Play Again?` = structure(c(5L, 2L, 4L, 5L, 
5L, 5L, 4L), levels = c("1 - Strongly Disagree", "2 - Disagree", 
"3 - Neutral", "4 - Agree", "5 - Strongly Agree"), class = c("ordered", 
"factor")), `Q20. More Code Orange` = structure(c(5L, 3L, 3L, 
4L, 4L, 5L, 5L), levels = c("1 - Strongly Disagree", "2 - Disagree", 
"3 - Neutral", "4 - Agree", "5 - Strongly Agree"), class = c("ordered", 
"factor"))), row.names = c(NA, -7L), class = "data.frame")

Solution

  • Seems like Chris' hunch is correct, there's a name conflict on "likert". You can use conflicts() to look for conflicts. If you call conflicts(details=TRUE), you'll see that both likert and HH has a function of that name. Which of the two functions you get when you call likert() typically depends on the order the packages were attached in; the last attached package will mask any conflicting names from previously attached packages, and you'll get a warning like:

    Attaching package: ‘HH’
    
    The following object is masked from ‘package:likert’:
    
        likert
    

    The general solution is to not have a bunch of packages attached that you don't need. Keep in mind that a package might load other packages it depends on, tidyverse for example will on its own load 30 packages. That's just asking for trouble. You can detach individual packages by using detach(). For example, running detach(package:HH) will clear the mask in question. In general, it's good practice to just restart R on occasion to keep things under control.
    If you rely on packages that have name conflicts, the most straight forward solution is to use the double colon operator as you did in likert::likert.bar.plot(). Although in this case it might have been used to access the function without loading the package namespace. Are you sure you attached likert at all? In any case, likert::likert() should solve it.

    library(likert)
    library(HH)
    
    thesis <- structure(list(`Q10. More Scenarios` = structure(c(5L, 3L, 4L, 
    5L, 5L, 4L, 4L), levels = c("1 - Strongly Disagree", "2 - Disagree", 
    "3 - Neutral", "4 - Agree", "5 - Strongly Agree"), class = c("ordered", 
    "factor")), `Q18. Play Again?` = structure(c(5L, 2L, 4L, 5L, 
    5L, 5L, 4L), levels = c("1 - Strongly Disagree", "2 - Disagree", 
    "3 - Neutral", "4 - Agree", "5 - Strongly Agree"), class = c("ordered", 
    "factor")), `Q20. More Code Orange` = structure(c(5L, 3L, 3L, 
    4L, 4L, 5L, 5L), levels = c("1 - Strongly Disagree", "2 - Disagree", 
    "3 - Neutral", "4 - Agree", "5 - Strongly Agree"), class = c("ordered", 
    "factor"))), row.names = c(NA, -7L), class = "data.frame")
    
    thesisLikert <- likert::likert(thesis)
    
    p <- likert::likert.bar.plot(thesisLikert, group.order = names(thesis), center = 3)
    p + labs(
      title="Future Expansion", 
      subtitle="Gamers", 
      x="Questions", 
      y="Percentage Response")
    

    enter image description here