简体   繁体   English

如何将映射函数应用于mutate和case_when函数中的动态列名?

[英]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以在ggplot2中绘制barplot:

    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. a_2_areaa_3_areaa_4_areaa_5_area使用for循环。

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 ; 另一方面,有可能使用purrr map进行相同的操作; 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 我认为在这里cut可能是一个不错的选择

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 也可以集成在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 或者,如果您更喜欢使用case_whenmutate_at可以使用case_whenmutate_at可以在多个列上运行相同的函数,因此您不必使用for循环

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")))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 R function 使用变量/列名称作为 arguments 使用 mutate 和 case_when 创建新变量时出错 - errors when using R function using variables/column names as arguments to create a new variable using mutate and case_when 如何将cross()函数与mutate()和case_when()结合起来,根据条件对多列中的值进行变异? - How to combine the across () function with mutate () and case_when () to mutate values in multiple columns according to a condition? 根据从不同列获得的值创建新列,使用 R 中的 mutate() 和 case_when 函数 - Creating a new column based on values obtained from different column, using mutate() and case_when function in R 在 R 中使用 Group by 和 mutate、case_when、any() 和 all() function - Use Group by with mutate, case_when, any() and all() function in R 在 mutate/case_when 中使用自定义矢量化 function 时出错 - Error in using custom vectorized function in mutate/case_when 用户定义的函数,在R中带有mutate和case_when - User defined function with mutate and case_when in R 尝试在 R 中的变异动词内执行 case_when function - Trying to perform a case_when function inside a mutate verb in R 使用 mutate 和 case_when 的用户定义函数 - User defined function using mutate & case_when case_when 在变异 function 内出错:输入必须是向量 - Error with case_when within mutate function: input must be vector Function 使用 mutate 和 case_when 创建二分法 var (0/1) - Function to create dichotomic var (0/1) using mutate and case_when
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM