我有一个大型数据集,我正在尝试减少焦点。作为其中的一部分,我有三个变量,五次测量。我每次都做了一个意思,所以我最终得到了五个变量(每次一个),我想找到它的内部一致性。当我尝试使用psych::alpha()
计算Cronbach的alpha时,我得到一个错误。
25行可重现的数据:
structure(list(catme_satis1a = c(4L, 4L, 5L, 5L, 5L, NA, 1L,
4L, 4L, 4L, 4L, 2L, 4L, 4L, 3L, 4L, 4L, 5L, 3L, 4L, 5L, 3L, 4L,
4L, 5L), catme_satis1b = c(4L, 4L, 4L, 5L, 5L, NA, 1L, 4L, 5L,
5L, 4L, 2L, 5L, 4L, 3L, 4L, 4L, 5L, 3L, 4L, 5L, 3L, 4L, 4L, 5L
), catme_satis1c = c(3L, 4L, 5L, 5L, 5L, NA, 1L, 4L, 3L, 4L,
4L, 2L, 4L, 5L, 3L, 4L, 4L, 5L, 3L, 4L, 5L, 3L, 4L, 4L, 5L),
catme_satis2a = c(4L, 4L, 4L, 5L, 5L, NA, 5L, 4L, 5L, NA,
NA, 3L, NA, 4L, 3L, 4L, 4L, 5L, 3L, NA, 5L, 5L, 4L, 4L, 5L
), catme_satis2b = c(4L, 4L, 5L, 5L, 5L, NA, 5L, 4L, 5L,
NA, NA, 3L, NA, 4L, 3L, 4L, 3L, 5L, 2L, NA, 5L, 5L, 4L, 4L,
5L), catme_satis2c = c(4L, 4L, 5L, 5L, 5L, NA, 5L, 4L, 5L,
NA, NA, 3L, NA, 4L, 3L, 4L, 3L, 5L, 3L, NA, 5L, 5L, 4L, 4L,
5L), catme_satis3a = c(4L, 4L, 4L, 5L, 5L, 5L, 4L, 4L, 5L,
5L, 3L, NA, 3L, 4L, 3L, NA, 4L, 5L, 3L, 5L, 5L, 5L, 4L, 5L,
5L), catme_satis3b = c(4L, 4L, 4L, 5L, 5L, 5L, 3L, 4L, 5L,
5L, 3L, NA, 3L, 4L, 3L, NA, 4L, 5L, 3L, 5L, 5L, 5L, 4L, 5L,
5L), catme_satis3c = c(4L, 5L, 5L, 5L, 5L, 4L, 4L, 4L, 5L,
5L, 3L, NA, 3L, 4L, 3L, NA, 4L, 5L, 4L, 5L, 5L, 5L, 4L, 4L,
5L), catme_satis4a = c(4L, 5L, 5L, 5L, 5L, 5L, 4L, 4L, 5L,
4L, 3L, 3L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, NA, 5L, 5L, 5L, 5L,
5L), catme_satis4b = c(4L, 4L, 5L, 5L, 5L, 4L, 4L, 4L, 5L,
4L, 3L, 3L, 2L, 4L, 3L, 4L, 5L, 5L, 4L, NA, 5L, 5L, 5L, 5L,
5L), catme_satis4c = c(4L, 4L, 5L, 5L, 5L, 4L, 4L, 4L, 5L,
3L, 3L, 3L, 2L, 4L, 3L, 5L, 4L, 4L, 4L, NA, 5L, 5L, 5L, 5L,
5L), catme_satis5a = c(5L, 4L, 5L, 5L, 5L, 5L, 5L, 4L, 5L,
4L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 4L, 5L, 5L, 1L, 5L,
5L), catme_satis5b = c(5L, 4L, 5L, 5L, 5L, 5L, 5L, 4L, 5L,
4L, 3L, 3L, 3L, 4L, 4L, 5L, 4L, 5L, 5L, 4L, 5L, 5L, 1L, 5L,
5L), catme_satis5c = c(5L, 4L, 5L, 5L, 5L, 5L, 5L, 4L, 5L,
4L, 3L, 3L, 2L, 4L, 4L, 5L, 4L, 3L, 5L, 4L, 5L, 5L, 1L, 5L,
5L)), class = "data.frame", row.names = c(NA, -25L), .Names = c("catme_satis1a",
"catme_satis1b", "catme_satis1c", "catme_satis2a", "catme_satis2b",
"catme_satis2c", "catme_satis3a", "catme_satis3b", "catme_satis3c",
"catme_satis4a", "catme_satis4b", "catme_satis4c", "catme_satis5a",
"catme_satis5b", "catme_satis5c"))
接下来,我尝试了这一点来获得第1列,第3列,第4列等的平均值(但是按名称):
library(dplyr)
df1 <- test %>%
rowwise() %>%
transmute(catme_satis1 = mean(c(catme_satis1a, catme_satis1b, catme_satis1c)),
catme_satis2 = mean(c(catme_satis2a, catme_satis2b, catme_satis2c)),
catme_satis3 = mean(c(catme_satis3a, catme_satis3b, catme_satis3c)),
catme_satis4 = mean(c(catme_satis4a, catme_satis4b, catme_satis4c)),
catme_satis5 = mean(c(catme_satis5a, catme_satis5b, catme_satis5c)))
最后,我想知道使用psych
包的这些变量的一致性:
library(psych)
alpha(df1)
出现此错误:
> alpha(df1)
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
我的数据框在打印时似乎是正确的,我应该能够获得这些值的一致性。为什么r
会抛出此错误?
答案 0 :(得分:0)
经过一番探索后,我找到了一种方法来完成这项工作。它涉及dplyr
输出,其余类别超出data.frame
。我以不同的方式创建了平均列,以便使用以下代码远离dplyr
(请注意,此代码名为df2
以便于以后进行比较):
df2 <- data.frame(
catme_satis1 = apply(test[, 1:3], 1, mean),
catme_satis2 = apply(test[, 4:6], 1, mean),
catme_satis3 = apply(test[, 7:9], 1, mean),
catme_satis4 = apply(test[, 10:12], 1, mean),
catme_satis5 = apply(test[, 13:15], 1, mean)
)
alpha(df2)
命令运行正常。这激发了我检查数据帧的一些事情。
我原来帖子中的df1
课程与此df2
不同:
> class(df1)
[1] "rowwise_df" "tbl_df" "tbl" "data.frame"
> class(df2)
[1] "data.frame"
此外,除非我强制将dplyr输出强制为数据帧,否则他们会认为是完全相同的!
> identical(df1, df2)
[1] FALSE
> identical(as.data.frame(df1), df2)
[1] TRUE
运行命令alpha(as.data.frame(df1))
有效并产生相同的结果。这里有两种解决方案:
dplyr
方法获取平均数据。这将数据保存为data.frame分类对象。as.data.frame()
函数时,使用alpha()
将对象强制转换为正确的类。或者将%>% as.data.frame()
添加到dplyr
mutate命令的末尾。