gpt4 book ai didi

r - R 中的李克特分组错误

转载 作者:行者123 更新时间:2023-12-04 10:53:04 24 4
gpt4 key购买 nike

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

最佳答案

此错误消息已在 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

关于r - R 中的李克特分组错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26983183/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com