【问题标题】:Psych alpha() function giving error when fed dplyr resultsPsych alpha() 函数在输入 dplyr 结果时给出错误
【发布时间】:2015-12-09 03:09:20
【问题描述】:

我有一个大型数据集,我正在尝试缩减以集中注意力。作为其中的一部分,我有三个变量,测量了五次。我已经把每次都变成了一个平均值,所以我最终得到了五个变量(每次一个),我想找到它们的内部一致性。当我尝试使用 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:6 等列的平均值(但按名称):

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 会抛出这个错误?

【问题讨论】:

  • 试试alpha(as.data.frame(df1))alpha 期待 data.framematrixclass(df1)"rowwise_df" "tbl_df" "tbl" "data.frame"
  • 经过一番探索后,我发现了这一点,并准备回答我自己的问题。你让我在我的帖子中间!

标签: r dplyr psych


【解决方案1】:

在做了一些探索之后,我找到了一种方法来完成这项工作。它涉及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)) 可以工作并产生相同的结果。这里有两种解决方案:

  1. 使用非dplyr 方法获取平均数据。这会将数据保存为 data.frame 类对象。
  2. 在运行alpha() 函数时,使用as.data.frame() 将对象强制转换为正确的类。或者将%&gt;% as.data.frame() 添加到dplyr mutate 命令的末尾。

【讨论】:

    猜你喜欢
    • 2019-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多