R语言 从应用于存储为因子的数据的评分系统计算“总”变量

w46czmvw  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(113)

我在R(和tidyverse)中使用的数据来自一份由11个问题组成的问卷,每个问题都在4分制的likert量表上回答:

  • 比正常少
  • 不比平常多
  • 比平常多
  • 比平常多得多

数据位于数据框中,参与者作为行,对每个问题的回答作为有序因子存储在各个列中。
下面的代码复制了当前存储的5行数据:

library(tidyverse)

df <- tibble(id  = c(1, 2, 3, 4, 5), q1  = c(3, 4, 2, 3, 3),
             q2  = c(4, 4, 2, 3, 2), q3  = c(3, 3, 2, 2, 3),
             q4  = c(2, 2, 3, 2, 1), q5  = c(3, 3, 3, 3, 3),
             q6  = c(4, 3, 2, 2, 2), q7  = c(1, 2, 2, 2, 2),
             q8  = c(3, 3, 3, 2, 1), q9  = c(3, 4, 4, 2, 1),
             q10 = c(2, 4, 3, 2, 1), q11 = c(2, 3, 2, 2, 1)) %>% 
  mutate(across(q1:q11, ~factor(.x,
                                levels = c(1, 2, 3, 4),
                                labels = c("Less than usual",
                                           "No more than usual",
                                           "More than usual",
                                           "Much more than usual"),
                                ordered = TRUE)))

str(df)

# tibble [5 × 12] (S3: tbl_df/tbl/data.frame)
#  $ id : num [1:5] 1 2 3 4 5
#  $ q1 : Ord.factor w/ 4 levels "Less than usual"<..: 3 4 2 3 3
#  $ q2 : Ord.factor w/ 4 levels "Less than usual"<..: 4 4 2 3 2
#  $ q3 : Ord.factor w/ 4 levels "Less than usual"<..: 3 3 2 2 3
#  $ q4 : Ord.factor w/ 4 levels "Less than usual"<..: 2 2 3 2 1
#  $ q5 : Ord.factor w/ 4 levels "Less than usual"<..: 3 3 3 3 3
#  $ q6 : Ord.factor w/ 4 levels "Less than usual"<..: 4 3 2 2 2
#  $ q7 : Ord.factor w/ 4 levels "Less than usual"<..: 1 2 2 2 2
#  $ q8 : Ord.factor w/ 4 levels "Less than usual"<..: 3 3 3 2 1
#  $ q9 : Ord.factor w/ 4 levels "Less than usual"<..: 3 4 4 2 1
#  $ q10: Ord.factor w/ 4 levels "Less than usual"<..: 2 4 3 2 1
#  $ q11: Ord.factor w/ 4 levels "Less than usual"<..: 2 3 2 2 1

我需要使用两种不同的评分系统来计算整个问卷以及两个选择问题的分量表。第一个分量表由问题1-7组成,第二个分量表由问题8-11组成。

  • 第一个评分系统(Likert)分别将值0、1、2和3分配给因子水平。
  • 第二评分系统(二进制)分别分配值0、0、1和1。

我如何使用两个评分系统计算这些总数,以获得6个(子)总数:total_likerttotal_binarytotal_ss1_likerttotal_ss1_binarytotal_ss2_likerttotal_ss2_binary

hfyxw5xn

hfyxw5xn1#

您可以首先使用acrossrecode(您可能也想选择replace)根据评分系统更新值,然后使用rowwise计算每个id的总得分:

df %>%
  mutate(across(starts_with("q"), ~ recode(.x, "Less than usual" = 0,
                                           "No more than usual" = 1, 
                                           "More than usual" = 2, 
                                           "Much more than usual" = 3), 
                .names = "likert_{.col}")) %>%
  mutate(across(starts_with("q"), ~ recode(.x, "Less than usual" = 0,
                                           "No more than usual" = 0, 
                                           "More than usual" = 1, 
                                           "Much more than usual" = 1), 
                .names = "binary_{.col}")) %>%
  rowwise(id) %>% mutate(total_likert = sum(c_across(likert_q1:likert_q11)),
                         total_ss1_likert = sum(c_across(likert_q1:likert_q7)),
                         total_ss2_likert = sum(c_across(likert_q8:likert_q11)),
                         total_binary = sum(c_across(binary_q1:binary_q11)),
                         total_ss1_binary = sum(c_across(binary_q1:binary_q7)),
                         total_ss2_binary = sum(c_across(binary_q8:binary_q11))) %>%
  select(id, total_likert, total_binary, total_ss1_likert, total_ss1_binary, total_ss2_likert, total_ss2_binary)

相关问题