Tidyverse--集成突变选择和案例何时使用likert比例

Tidyverse--集成突变选择和案例何时使用likert比例,r,select,dplyr,mutate,R,Select,Dplyr,Mutate,我是一个tidyverse用户,我面临着执行我想象中的简单命令的困难 我有一个数据集,其中一些变量类似于ERT scales,现在,我想为这些特定的变量集指定一些文本标签。要做到这一点,我想最好的过程是变异,然后选择我想要改变的变量,然后告诉R新值是什么 在我看来,这似乎是合乎逻辑的。然而,在我想要做的事情和我正在创建的R代码之间有一座桥梁 我希望有人能帮我解决这个问题 以下代码是一个可复制的示例: library(tidyverse) ds <- data.frame(sex=c(0,1

我是一个tidyverse用户,我面临着执行我想象中的简单命令的困难

我有一个数据集,其中一些变量类似于ERT scales,现在,我想为这些特定的变量集指定一些文本标签。要做到这一点,我想最好的过程是变异,然后选择我想要改变的变量,然后告诉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"))
我没有发现任何以前的消息询问相同的问题,因此,我创建了这个新线程。请让我知道此消息是否重复以排除它

非常感谢。

我们可以在

但是,这可以变得更简单,因为值是整数,所以使用整数值作为索引,以我们想要更改的相同顺序传递字符串向量

v1 <- c('Strongly disagree', 'Disagree', 'Neutral', 'Agree', 'Strongly agree')
ds %>%
   mutate_at(vars(starts_with('question')),
         funs(v1[.]))
我们可以把它用在变种上

但是,这可以变得更简单,因为值是整数,所以使用整数值作为索引,以我们想要更改的相同顺序传递字符串向量

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

您还可以使用以下因素:

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为基数翻译:


您还可以使用以下因素:

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你再次帮助我!我感到尴尬的是,在问这个问题之前的一周左右,我自己也在试图找出这个答案。多谢@路易斯:掌握一门新语言需要一些时间。不要感到尴尬。继续练习。它将在短时间内为你变得简单,为那些关心它的人翻译:cols_lgl