简体   繁体   中英

How to apply map function to dynamic column names in mutate and case_when function?

This is the data frame that I have:

df <- data.frame(
  id       = c(1,2,3,4,5),
  a_1_area = c(3,10,4,0,15),
  a_2_area = c(2,1,1,0,3),
  a_3_area = c(12,3,0,3,1),
  a_4_area = c(9,7,8,0,0),
  a_5_area = c(1,2,0,2,2)
  )

Self-defined range is added to the df to draw barplot in ggplot2:

    df %>% mutate(a_1_range=case_when(
  a_1_area == 0 ~ "0",
  a_1_area >= 1  & a_1_area < 5 ~ "1-4",
  a_1_area >= 5  & a_1_area <10 ~ "5-9",
  a_1_area >= 10 & a_1_area <15 ~ "10-14",
  a_1_area >= 15                ~ "15-",
  TRUE ~ "999")
  )

Output:

  id a_1_area a_2_area a_3_area a_4_area a_5_area a_1_range
1  1        3        2       12        9        1       1-5
2  2       10        1        3        7        2      6-10
3  3        4        1        0        8        0       1-5
4  4        0        0        3        0        2         0
5  5       15        3        1        0        2     11-15

I continued this operation to the remaining columns; a_2_area , a_3_area , a_4_area and a_5_area using for loop.

for (i in 1:5) {
  df %>% mutate(!!colname[i]:=case_when(
    !!sym(varname[i]) == 0                          ~ "0",
    !!sym(varname[i]) >= 1  & !!sym(varname[i]) < 5 ~ "1-4",
    !!sym(varname[i]) >= 5  & !!sym(varname[i]) <10 ~ "5-9",
    !!sym(varname[i]) >= 10 & !!sym(varname[i]) <15 ~ "10-14",
    !!sym(varname[i]) >= 15                          ~ "15-",
    TRUE ~ "999")) -> df
  }

Output:

  id a_1_area a_2_area a_3_area a_4_area a_5_area a_1_range a_2_range a_3_range a_4_range
1  1        3        2       12        9        1       1-4       1-4     10-14       5-9
2  2       10        1        3        7        2     10-14       1-4       1-4       5-9
3  3        4        1        0        8        0       1-4       1-4         0       5-9
4  4        0        0        3        0        2         0         0       1-4         0
5  5       15        3        1        0        2       15-       1-4       1-4         0
  a_5_range
1       1-4
2       1-4
3         0
4       1-4
5       1-4

On the other hand, there may be a possibility to do the same operation using map in purrr ; however I still cannot get its possible application to dynamic variables. Could you have any suggestions on that?

I think cut could be a good option here

cols <- grep("area$", names(df), value = TRUE)

df[paste0(cols, "_range")] <- lapply(df[cols], function(x) cut(x,
           breaks = c(0, 1, 5, 10, 15, Inf), 
           labels = c("0", "1-4", "5-9", "10-14", "15-"), include.lowest = TRUE))

which can also be integrated in dplyr

library(dplyr)
df %>%
   mutate_at(vars(ends_with("area")), list(range = ~cut(., 
                   breaks = c(0, 1, 5, 10, 15, Inf), 
            labels = c("0", "1-4", "5-9", "10-14", "15-"), include.lowest = TRUE)))

Or if you prefer to use case_when , mutate_at can be used which would run the same function on multiple columns so that you don't have to use for loop

df %>% mutate_at(vars(ends_with("area")), list(area = 
       ~case_when(. == 0 ~ "0",
                  . >= 1  & . < 5 ~ "1-4",
                  . >= 5  & . <10 ~ "5-9",
                  . >= 10 & . <15 ~ "10-14",
                  . >= 15 ~ "15-",
                  TRUE ~ "999")))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM