使用标准评估,Forcats和Stringr重新编码多个满意度量表变量的功能

时间:2017-03-01 02:37:49

标签: r stringr tidyverse lazyeval

library(tidyverse)
library(stringr)
library(lazyeval)

以下是简单数据框示例的数据......

Respondent<-c("Respondent1","Respondent2","Respondent3","Respondent4","Respondent5")
Sat1<-c("1 Extremely dissatisfied","2 Moderately dissatisfied","2 Moderately Dissatisfied","4 Neutral","7 Extrmely satified")
Sat2<-c("7 Extremely Satisfied","2. Moderately dissatisfied","4 Neutral","3  Slightly dissatisfied","3 Slightly Dissatisfied")
Sat3<-c("1 Extremely dissatisfied","7 Extremely satisfied","6 Moderately satisfied","4. Neutral","3 Slightly dissatisfied")
Pet<-c("Cat","Cat","Dog","Hamster","Rabbit")

df <- data_frame(Respondent,Sat1,Sat2,Sat3,Pet)

以下代码是将满意度得分列重新编码为满意,不满意和中立三类。

df %>% 
mutate_at(vars(starts_with("Sat")), 
     funs(fct_collapse(factor(str_sub(., 1, 1), levels = as.character(1:7)),
                          Satisfied = c("7","6","5"),
                          Dissatisfied =c ("3", "2","1"),
                          Neutral = "4")))

然而,我的真实例子涉及重新编码相同的满意度 多个文件,每个文件具有不同数量的满意度标度列。所以我想把它包装成一个允许我输入数据帧名称的函数,以及要重新编码的任意数量的列。下面是我尝试使用的代码的一个变体,但我无法让它工作。我一直在玩.dots和“......”,但找不到任何有用的东西。

 REC<-function(data,...){
data %>% 
 mutate_at(vars(...), 
     funs(fct_collapse(factor(str_sub(., 1, 1), levels = as.character(1:7)),
                   Satisfied = c("7","6","5"),
                   Dissatisfied =c ("3", "2","1"),
                   Neutral = "4")))
                   }

我应该使用mutate_at进行标准评估吗?另外,我必须使用.dots与...?如果标准评估不能与mutate_at一起使用,我可以使用其他函数/技术来实现相同的最终目标,最好是在tidyverse中。

1 个答案:

答案 0 :(得分:1)

starts_with("Sat")适用于您的所有文件吗?如果是这样,无论有多少列以“Sat”开头,该函数都会起作用。

REC <- function(data){
  data %>% 
    mutate_at(vars(starts_with("Sat")),
                   funs(fct_collapse(factor(str_sub(., 1, 1), levels = as.character(1:7)),
                                     Satisfied=c("7","6","5"),
                                     Dissatisfied=c("3", "2","1"),
                                     Neutral="4")))
} 

如果要传递要更改的列的索引,可以尝试:

REC <- function(data, variable){
  data %>% 
    mutate_at(vars(variable),
              funs(fct_collapse(factor(str_sub(., 1, 1), levels = as.character(1:7)),
                                    Satisfied=c("7","6","5"),
                                    Dissatisfied=c("3", "2","1"),
                                    Neutral="4")))
}  

然后REC(df, 2:4)将为您提供此输出

# A tibble: 5 × 5
   Respondent         Sat1         Sat2         Sat3     Pet
        <chr>       <fctr>       <fctr>       <fctr>   <chr>
1 Respondent1 Dissatisfied    Satisfied Dissatisfied     Cat
2 Respondent2 Dissatisfied Dissatisfied    Satisfied     Cat
3 Respondent3 Dissatisfied      Neutral    Satisfied     Dog
4 Respondent4      Neutral Dissatisfied      Neutral Hamster
5 Respondent5    Satisfied Dissatisfied Dissatisfied  Rabbit