R中的李克特分组错误

时间:2014-11-17 22:28:33

标签: r grouping lapply

我正在尝试在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)

3 个答案:

答案 0 :(得分: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 :(得分:1)

在最新版本的Likert上,加载“reshape”不再能解决这个问题。

要使其正常工作,您需要从包中分离“重塑”,然后加载reshape2。

detach('package:reshape')
library(reshape2)

答案 2 :(得分:0)

嗯。我今天也有同样的问题。然而加载重塑并没有解决它。我在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