1

I have data with 5 columns in a variable called studentData. Each column has 326 rows, except one which has 3 rows missing. Each column is a 5 point likert value, from the set mylevels <- c('Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree')

When I print the number of levels in each column it gives me value as 6 for the second column (studentData$Increased.confidence), because it has 3 missing values which R interprets as another factor for this column.

> sapply(studentData, function(x) { length(levels(x)) } ) # The number of levels in each factor
              ï..Increased.engagement                  Increased.confidence               Improved.writing.skills 
                                    5                                     6                                     5 
   Made.useful.contribution.to.course Should.keep.games.for.future.students 
                                    5                                     5 

Because of this I get the error stating that the number of levels should be same for likert function to work. How should I handle those 3 missing values?

> studentLikert <- likert(studentData)
Error in likert(studentData) : 
  All items (columns) must have the same number of levels
Roman Luštrik
  • 69,533
  • 24
  • 154
  • 197
Vipin Verma
  • 5,330
  • 11
  • 50
  • 92

1 Answers1

1

Try this: define your columns as factor making sure that you exclude missing values from factor level definition by using exclude=' '

a <- c('A','B','C','','A')
b <- c('A','B','A','C','B')
df <- data.frame(a,b)

mylevels <- c('A', 'B', 'C')
df <- as.data.frame(lapply(df,function(x) {factor(x,levels=mylevels, exclude="")}))
Bea
  • 1,110
  • 12
  • 20