Home > front end >  Factorizing leads to NA
Factorizing leads to NA

Time:10-24

I have the following table. With 'a' and 'b' on a likert scale from 1-5.

type a b
typeOne 2 3
typeTwo 4 4
typeOne 5 2

Now I try to visualize the data as seen here in two groups (typeOne, typeTwo) and with 'a' and 'b' on the y-axis.

Likert Example Image

Unfortunately when I run my factor code all numeric values vanish from the table and get replaced by 'NA'.

My code is:

df <- as.data.frame(data) 

factorLevels <- c('Strongly Disagree', 'Disagree', 'Neither', 'Agree', 'Strongly Agree') 

df[2:3] <- lapply(df[2:3], factor, levels=factorLevels) 

library(likert)
df_likert <- likert(df[,c(2:3)], grouping = df$type) 
plot(df_likert)

When I run the plot command I get the following error:

argument must be coercible to non-negative integer

CodePudding user response:

You're confusing levels= with labels=.

fac <- c('a', 'b')

f_labels <- c('Strongly Disagree', 'Disagree', 'Neither', 'Agree', 
              'Strongly Agree')

d[fac] <- lapply(d[fac], factor, levels=1:5, labels=f_labels)

library(likert)
d_likert <- likert(d[fac], grouping=d$type) 
plot(d_likert)

enter image description here


Data:

d <- read.table(text='type  a   b
typeOne     2   3
typeTwo     4   4
typeOne     5   2', header=TRUE)
  •  Tags:  
  • r
  • Related