R中的李克特分组错误

jon*_*nas 3 grouping r lapply

我正在尝试在Likert函数中使用分组参数但是重新发现错误:

lik <- likert(df2, grouping = df$CAR)

Error in sum(x) : invalid 'type' (character) of argument
Run Code Online (Sandbox Code Playgroud)

这是我的简单代码:

library(likert)

df<- fread("C:/R/temp/likert_test.csv", select = 1:6)
df <- as.data.frame(df)

col_names <- names(df[1:6])
df[,col_names] <- lapply(df[,col_names] , factor)

str(df)
'data.frame':   19331 obs. of  6 variables:
 $ CAR                                                     : Factor w/ 34 levels "Alfa Romeo","Audi",..: 4 4 4 4 3 3 3 3 4 4 ...
 $ E1.Overall Satisfaction                                 : Factor w/ 10 levels "1","2","3","4",..: 8 8 8 9 9 7 10 8 7 10 ...
 $ E2.Exterior Styling                                     : Factor w/ 10 levels "1","2","3","4",..: 9 8 8 10 8 4 10 8 7 10 ...
 $ E2.Overall Quality                                      : Factor w/ 10 levels "1","2","3","4",..: 8 8 7 10 10 8 10 8 8 10 ...
 $ E2.Interior Styling                                     : Factor w/ 10 levels "1","2","3","4",..: 9 6 9 10 9 8 9 8 7 10 ...
 $ E2.Quality Of Interior And Materials Used Inside The Car: Factor w/ 10 levels "1","2","3","4",..: 7 6 7 10 10 8 10 7 7 10 ...


df2 <- df[,2:5]

lik <- likert(df2, grouping = df$CAR)
Run Code Online (Sandbox Code Playgroud)

jaz*_*rro 5

已在Github上报告此错误消息(https://github.com/jbryer/likert/issues/26).解决方案是上传reshape包.

library(likert)
library(reshape)

# I created a sample. Please provide a sample like this from next time.
foo <- data.frame(car = rep(c("Toyota", "BMW", "Ford"), times = 5),
                  satisfaction = c(1,3,4,7,7,6,2,3,5,5,5,2,4,1,7),
                  quality = c(1,1,3,5,4,3,6,4,3,6,6,1,7,2,7))

# Convert all columns to factor
foo[1:3] <- lapply(foo[1:3], as.factor)

likert(foo[,c(2:3)], grouping = foo$car)

#   Group         Item  1  2  3  4  5  6  7
#1    BMW satisfaction 20  0 40  0 20  0 20
#2    BMW      quality 20 20  0 40  0 20  0
#3   Ford satisfaction  0 20  0 20 20 20 20
#4   Ford      quality 20  0 60  0  0  0 20
#5 Toyota satisfaction 20 20  0 20 20  0 20
#6 Toyota      quality 20  0  0  0 20 40 20
Run Code Online (Sandbox Code Playgroud)