Tidyverse-整合变异选择和大小写何时缩放

时间:2018-08-21 13:33:05

标签: r select dplyr mutate

我是 tidyverse 用户,在执行我认为简单的命令时遇到了困难。

我有一个数据集,其中一些变量是李克特量表,现在,我想为这些特定的变量集分配一些文本标签。为此,我想最好的过程涉及到变异,然后选择我要更改的变量,然后告诉R什么是新值

在我看来,这是合乎逻辑的。但是,我想要做的事情和正在创建的R代码之间架起了一座桥梁。

我希望有人能帮助我解决这个问题。

以下代码是可复制的示例:

library(tidyverse)
ds <- data.frame(sex=c(0,1), age=rnorm(n = 100, mean = 7, sd=2),
                 question1_1 = sample(1:5),
                 question1_2 = sample(1:5),
                 question1_3 = sample(1:5),
                 question1_4 = sample(1:5),
                 question1_5 = sample(1:5))
ds <- ds %>% mutate(
  select %>% starts_with("question1_") %>% 
  case_when(. == 1 ~ "Strongly disagree",
            . == 2 ~ "Disagree",
            . == 3 ~ "Neutral",
            . == 4 ~ "Agree",
            . == 5 ~ "Strongly agree"))

我没有找到任何先前的消息问同样的问题,因此,我创建了这个新线程。请让我知道此消息是否重复以排除它。

非常感谢。

2 个答案:

答案 0 :(得分:3)

我们可以在mutate_at

中使用它
ds %>%
   mutate_at(vars(starts_with('question')), 
         funs(case_when(.==1 ~ "Strongly disagree",
                        .==2 ~ "Disagree",
                        .==3 ~ "Neutral",
                        .==4 ~ "Agree",
                        .==5 ~ "Strongly agree")))

但是,由于值是整数,因此可以变得更简单,因此可以按照我们希望使用整数值作为索引来更改的顺序传递字符串vector

v1 <- c('Strongly disagree', 'Disagree', 'Neutral', 'Agree', 'Strongly agree')
ds %>%
   mutate_at(vars(starts_with('question')),
         funs(v1[.]))

答案 1 :(得分:1)

您还可以使用以下因素:

library(tidyverse)
ds %>%
  mutate_at(vars(starts_with('question')), factor, 
                 labels= c("Strongly disagree","Disagree","Neutral","Agree", "Strongly agree")) %>%
  head

#   sex      age       question1_1       question1_2       question1_3       question1_4       question1_5
# 1   0 6.518414             Agree           Neutral          Disagree           Neutral             Agree
# 2   1 4.972210           Neutral          Disagree    Strongly agree Strongly disagree Strongly disagree
# 3   0 6.422792 Strongly disagree    Strongly agree             Agree          Disagree    Strongly agree
# 4   1 9.839967          Disagree             Agree           Neutral    Strongly agree           Neutral
# 5   0 7.518809    Strongly agree Strongly disagree Strongly disagree             Agree          Disagree
# 6   1 8.446730             Agree           Neutral          Disagree           Neutral             Agree

R为基础翻译:

cols_lgl <- startsWith(names(ds),"question")
ds[cols_lgl] <- lapply(ds[cols_lgl],factor, labels= c("Strongly disagree","Disagree","Neutral","Agree", "Strongly agree"))