R中的Likert分组错误

3
我正在尝试在likert函数中使用分组参数,但是出现了错误:
lik <- likert(df2, grouping = df$CAR)

Error in sum(x) : invalid 'type' (character) of argument

这是我的简单代码:

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)

谢谢你提出这个问题,我今天也遇到了完全相同的问题... - Produnis
3个回答

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

1
在最新版本的likert中,加载“reshape”不再解决这个问题。
为了使其正常工作,您需要从包中分离“reshape”,并加载reshape2。
detach('package:reshape')
library(reshape2)

0

嗯,我今天也有同样的问题。然而,加载reshape并没有解决我的问题。我在Windows 7上运行R 3.2.2。

#rerunning the above code
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)

#####Results in
> library(likert)
Loading required package: ggplot2
Loading required package: xtable
> 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)

Error in fix.by(by.y, y) : 'by' must specify uniquely valid columns

请将评论发布为评论,而不是答案。 - Brino
抱歉,我是Stack的新手 - 没有足够的声望在其他帖子上发表评论。我按照建议从GitHub重新安装了Likert,但仍然存在同样的问题。 - Mike Babyak
看起来包已经改变了 - 现在你需要分离“reshape”,以便它可以使用reshape2。 - micturalgia

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接