我有一个带有类别变量的重复测量设计。假设参与者在第1点和第2点报告了他们的态度(“批准”,“中立”,“不赞成”)。然后,我创建了一个包含响应组合频率的表。
# setup
set.seed(123)
library(tidyverse)
# made-up data
survey.data <- data.frame(
`1st survey` = c('Approve', 'Approve', 'Neutral', 'Approve'),
`2nd survey` = c('Approve', 'Disapprove', 'Approve', 'Neutral'),
`Counts` = c(79, 15, 86, 10),
check.names = FALSE
) %>%
tidyr::uncount(., Counts) %>%
tibble::as_tibble()
# table of counts
table("1" = survey.data$`1st survey`, "2" = survey.data$`2nd survey`)
#> 2
#> 1 Approve Disapprove Neutral
#> Approve 79 15 10
#> Neutral 86 0 0
请注意,由于没有人在时间点1选择选项"Disapprove"
,因此表中缺少该选项。当然,这是预期的。但是我要使用的功能(rcompanion::cohenG
)期望表中的行和列数相等。所以我想知道如何更改上面的代码以在此处获得预期的输出-
#> 2
#> 1 Approve Disapprove Neutral
#> Approve 79 15 10
#> Disapprove 0 0 0
#> Neutral 86 0 0
答案 0 :(得分:2)
您可以对三个已知水平的因子进行响应
levels <- c("Disapprove", "Neutral", "Approve")
survey.data <- survey.data %>%
mutate_at(vars(`1st survey`,
`2nd survey`),
factor, levels = levels)
table(survey.data$`1st survey`,
survey.data$`2nd survey`)
#>
#> Disapprove Neutral Approve
#> Disapprove 0 0 0
#> Neutral 0 0 86
#> Approve 15 10 79
由reprex package(v0.2.1)于2019-03-08创建