重新编码多个李克特量表——任何包?
Recoding Multiple Likert Scales-- Any packages?
目前,我正在将我的 Likert 类型量表重新编码为数值。但是,我在这个数据集中有许多不同尺度的项目。例如:Q2.1_1:Q2.1_16 是一个采用 Likert 量表的量表,其调式与其他调查不同。目前,我正在像这样手动输入每个重新编码:
final$Q2.1_1rc <- as.numeric(recode(
final$Q2.1_1,
"Very slightly or not at all" = 1,
"A little" = 2,
"Moderately" = 3,
"Quite a bit" = 4,
"Extremely" = 5
))
然后我 C/P 继续更改变量的名称。但是,我有一个很大的数据集,因此手动执行此操作会很麻烦。谁能帮我用更短的方法来编写这个代码?有没有帮助解决这个问题的软件包?也许是一个功能?
谢谢!
您可以一次重新编码多个变量的一种方法是使用 dplyr
包中的 mutate_at
函数。
示例数据
library(dplyr)
set.seed(123)
resp <- c("Very slightly or not at all", "A little", "Moderately", "Quite a bit", "Extremely")
final <- tibble(Q2.1_1 = sample(resp, 6, replace = TRUE),
Q2.2_1 = sample(resp, 6, replace = TRUE))
解决方案
假设你想在 final
中开始的变量的变量名都以 "Q2"
开头,你可以这样做:
final %>%
mutate_at(vars(starts_with("Q2")),
funs("rc" = recode(.,
"Very slightly or not at all" = 1,
"A little" = 2,
"Moderately" = 3,
"Quite a bit" = 4,
"Extremely" = 5)))
#> # A tibble: 6 x 4
#> Q2.1_1 Q2.2_1 Q2.1_1_rc Q2.2_1_rc
#> <chr> <chr> <dbl> <dbl>
#> 1 A little Moderately 2 3
#> 2 Quite a bit Extremely 4 5
#> 3 Moderately Moderately 3 3
#> 4 Extremely Moderately 5 3
#> 5 Extremely Extremely 5 5
#> 6 Very slightly or not at all Moderately 1 3
如 official documentation, the best place to get started with dplyr
is the data import chapter of the R for Data Science book while specific examples related to the use of mutate_at
can be found here 中所述。
目前,我正在将我的 Likert 类型量表重新编码为数值。但是,我在这个数据集中有许多不同尺度的项目。例如:Q2.1_1:Q2.1_16 是一个采用 Likert 量表的量表,其调式与其他调查不同。目前,我正在像这样手动输入每个重新编码:
final$Q2.1_1rc <- as.numeric(recode(
final$Q2.1_1,
"Very slightly or not at all" = 1,
"A little" = 2,
"Moderately" = 3,
"Quite a bit" = 4,
"Extremely" = 5
))
然后我 C/P 继续更改变量的名称。但是,我有一个很大的数据集,因此手动执行此操作会很麻烦。谁能帮我用更短的方法来编写这个代码?有没有帮助解决这个问题的软件包?也许是一个功能?
谢谢!
您可以一次重新编码多个变量的一种方法是使用 dplyr
包中的 mutate_at
函数。
示例数据
library(dplyr)
set.seed(123)
resp <- c("Very slightly or not at all", "A little", "Moderately", "Quite a bit", "Extremely")
final <- tibble(Q2.1_1 = sample(resp, 6, replace = TRUE),
Q2.2_1 = sample(resp, 6, replace = TRUE))
解决方案
假设你想在 final
中开始的变量的变量名都以 "Q2"
开头,你可以这样做:
final %>%
mutate_at(vars(starts_with("Q2")),
funs("rc" = recode(.,
"Very slightly or not at all" = 1,
"A little" = 2,
"Moderately" = 3,
"Quite a bit" = 4,
"Extremely" = 5)))
#> # A tibble: 6 x 4
#> Q2.1_1 Q2.2_1 Q2.1_1_rc Q2.2_1_rc
#> <chr> <chr> <dbl> <dbl>
#> 1 A little Moderately 2 3
#> 2 Quite a bit Extremely 4 5
#> 3 Moderately Moderately 3 3
#> 4 Extremely Moderately 5 3
#> 5 Extremely Extremely 5 5
#> 6 Very slightly or not at all Moderately 1 3
如 official documentation, the best place to get started with dplyr
is the data import chapter of the R for Data Science book while specific examples related to the use of mutate_at
can be found here 中所述。