在R中分组的李克特问题

时间:2014-11-21 19:16:47

标签: r grouping lapply

我最近问了一个关于使用likert包进行分组的问题,以便可视化我的数据。我得到的答案在我的示例数据集上运行良好。

链接:likert grouping question 以下是解决方案提案:

foo <- data.frame(car = rep(c("Toyota", "BMW", "Ford"), times = 10),
                  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),
                  loyalty = c(1,1,3,5,4,3,9,4,3,10,6,1,7,2,8) )



foo[2:4] <- lapply(foo[2:4], factor, levels=1:10)
likt <- likert(foo[,c(2:4)], grouping = foo$car)
plot(likt)

然而,当我在我的数据集上尝试这个时,我收到一条错误消息,我无法理解:

library(reshape)
library(likert)


setwd("C:/mypath")
df<- fread("myfile.csv", select = 1:9)
df <- as.data.frame(df)



#reduce the colnames length
names(df) <- sub("^(.+[:])([^.]+)$", "\\2", names(df))

col_names <- names(df[1:9])
df[,col_names] <- lapply(df[,col_names] , factor, levels = 1:10)
str(df)

df[2:9] <- lapply(df[2:9], factor, levels=1:10)
lik2 <- likert(df[,c(2:9)], grouping = df$Make)
plot(lik2)

错误消息:

Error in `$<-.data.frame`(`*tmp*`, "low", value = c(NA_real_, NA_real_,  : 
  replacement has 8 rows, data has 0

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

发现菜鸟错误!!

应该从第二列开始,因为数值数据从那里开始..简单就是那样..

col_names <- names(df[2:9])