用户定义的函数使用mutate和case_when

时间:2019-12-01 01:48:39

标签: r dplyr user-defined-functions mutate

我有学校数据显示每个种族组中的学生百分比(黑人学生/总学生)。

我的示例数据如下:

School  Race    perc_race
1   EnrollBlack 3
2   EnrollBlack 67
3   EnrollWhite 4
4   EnrollWhite 8
5   EnrollHis   55
6   EnrollHis   88
7   EnrollAsian 43
8   EnrollAsian 34

我正在尝试为每个种族创建一个虚拟变量,以显示一所学校所处的位置。例如,如果一所学校有20%的黑人学生,则黑人的值为1,因为该学校属于第一等项。如果学校有67%的黑色,则它们将落入第三等分线,并且在黑色栏中会显示“ 3”。

School  Race    Percent_race    black   white   hisp    asian
1   EnrollBlack       3         1           
2   EnrollBlack       67        3           
3   EnrollWhite       4                    1        
4   EnrollWhite       8                    1        
5   EnrollHis         55                          2 
6   EnrollHis         88                          3 
7   EnrollAsian       43                                  2
8   EnrollAsian 3     4                                   2

我可以为数据集中的每个比赛重复此代码块,但是可以通过相应地替换比赛(即“ EnrollWhite”,“ EnrollHis” ...)

  mutate(black = case_when(race=='EnrollBlack' & perc_race>66.66 ~"3",
                           race=='EnrollBlack' & perc_race>33.33 ~"2",
                           race=='EnrollBlack' & perc_race<=33.33 ~"1"))

我没有复制粘贴5次,而是想出一个用户定义的函数,例如this。

  def_tercile <- function(x,y){
  mutate(y = case_when(race=='x' & perc_race>66.66 ~"3",
                           race=='x' & perc_race>33.33 ~"2",
                           race=='x' & perc_race<=33.33 ~"1"))
  }

数据%>%def_tercile(EnrollWhite,White)将返回一个新列,该列定义了学校所属的“白色”工具。

我不确定dplyr是否可以通过这种方式在函数中使用(运行函数时,它会不断抛出错误)。关于如何处理此问题有任何想法吗?

1 个答案:

答案 0 :(得分:1)

library("tidyverse")

df <- read_table2("School  Race    perc_race
1   EnrollBlack 3
2   EnrollBlack 67
3   EnrollWhite 4
4   EnrollWhite 8
5   EnrollHis   55
6   EnrollHis   88
7   EnrollAsian 43
8   EnrollAsian 34")

要获得韧性,我们只需将33.33除以并加上1

df %>%
  group_by(Race) %>%
  mutate(
    tercile = 1 + perc_race %/% (100/3)
  )
#> # A tibble: 8 x 4
#> # Groups:   Race [4]
#>   School Race        perc_race tercile
#>    <dbl> <chr>           <dbl>   <dbl>
#> 1      1 EnrollBlack         3       1
#> 2      2 EnrollBlack        67       3
#> 3      3 EnrollWhite         4       1
#> 4      4 EnrollWhite         8       1
#> 5      5 EnrollHis          55       2
#> 6      6 EnrollHis          88       3
#> 7      7 EnrollAsian        43       2
#> 8      8 EnrollAsian        34       2

然后我们可以使用pivot_wider为他们提供自己的列。

df %>%
  group_by(Race) %>%
  mutate(
    tercile = 1 + perc_race %/% (100/3),
    simple_race = Race %>% str_replace("Enroll", "") %>% str_to_lower()
  ) %>%
  pivot_wider(names_from = simple_race, values_from = tercile)
#> # A tibble: 8 x 7
#> # Groups:   Race [4]
#>   School Race        perc_race black white   his asian
#>    <dbl> <chr>           <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1      1 EnrollBlack         3     1    NA    NA    NA
#> 2      2 EnrollBlack        67     3    NA    NA    NA
#> 3      3 EnrollWhite         4    NA     1    NA    NA
#> 4      4 EnrollWhite         8    NA     1    NA    NA
#> 5      5 EnrollHis          55    NA    NA     2    NA
#> 6      6 EnrollHis          88    NA    NA     3    NA
#> 7      7 EnrollAsian        43    NA    NA    NA     2
#> 8      8 EnrollAsian        34    NA    NA    NA     2

要回答有关dplyr函数的问题,可以这样编写要定义的函数。对于将race_name作为列名进行处理的函数,我们需要使用!!:=语法。

def_tercile <- function(data, race_value, race_name) {
  mutate(data,
    !!race_name := case_when(
      Race == race_value & perc_race > 66.66 ~ "3",
      Race == race_value & perc_race > 33.33 ~"2",
      Race == race_value & perc_race <= 33.33 ~"1")
  )
}

df %>%
  def_tercile("EnrollBlack", "black") %>%
  def_tercile("EnrollWhite", "white") %>%
  def_tercile("EnrollHis", "his") %>%
  def_tercile("EnrollAsian", "asian")
#> # A tibble: 8 x 7
#>   School Race        perc_race black white his   asian
#>    <dbl> <chr>           <dbl> <chr> <chr> <chr> <chr>
#> 1      1 EnrollBlack         3 1     NA    NA    NA   
#> 2      2 EnrollBlack        67 3     NA    NA    NA   
#> 3      3 EnrollWhite         4 NA    1     NA    NA   
#> 4      4 EnrollWhite         8 NA    1     NA    NA   
#> 5      5 EnrollHis          55 NA    NA    2     NA   
#> 6      6 EnrollHis          88 NA    NA    3     NA   
#> 7      7 EnrollAsian        43 NA    NA    NA    2    
#> 8      8 EnrollAsian        34 NA    NA    NA    2