我在一个名为studentData的变量中有5列的数据。每列有326行,但其中一列缺少3行。每一列都是来自集合mylevels <- c('Strongly disagree', 'Disagree', 'Neither agree nor disagree', 'Agree', 'Strongly agree')的5点likert值。
当我在每一列中打印级别数时,它为第二列(studentData$Increased.confidence)提供6的值,因为它有3个缺失的值,R将其解释为该列的另一个因素。
> 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 正因为如此,我得到了一个错误,说明级别的数量应该是相同的,likert函数才能工作。我应该如何处理这3个缺失的值?
> studentLikert <- likert(studentData)
Error in likert(studentData) :
All items (columns) must have the same number of levels发布于 2017-04-27 09:45:22
尝试如下:将列定义为因素,确保使用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="")}))https://stackoverflow.com/questions/43653459
复制相似问题