我们能否实际将两组多个变量传递给dplyr中的mutate [英] Can we actually pass two sets of multiple variables into mutate across in dplyr

查看:59
本文介绍了我们能否实际将两组多个变量传递给dplyr中的mutate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题,尽管有三个答案使我感到疑惑我正在思考这个问题.尽管我知道可以用其他方法解决问题(特别是使用purrr或应用一组函数),但是我不确定可以通过 mutate(across(... 我在这里为了清楚起见重现该问题.注意:我不是在寻找答案,而只是在怀疑我是否可以通过mutate/across传递两组变量 .>

有两组变量(一组不带后缀,一组带后缀).

  df<-tibble(a = c(0,1,0,0,0),a_avail = c(1,1,1,0,0),b = c(1,1,1,0,0),b_avail = c(1,0,0,1,0))#小动作:5 x 4a a_avail b b_avail< dbl>< dbl>< dbl>< dbl>1 0 1 1 12 1 1 1 03 0 1 1 04 0 0 0 15 0 0 0 0 

现在,如果我们要变异一组变量,请说(a和b),但要与另一组变量串联进行比较.也就是说,当列a发生变异时,它可以使用其相应的变量a_avail,而列b发生变异时,它可以使用 b_avail 等,最多n个变量.

除了OP之外,我还尝试了这些代码

  df%>%mutate(d = row_number())%>%mutate(across(.cols = c(a_avail,b_avail),.fns =〜case_when(.x == 1〜{str_replace(cur_column(),"_avail",")[d]},.x == 0〜NA_character_),.names ="{.col} _new")) 

OR

  df%>%mutate(across(.cols = c(a,b),.fns =〜case_when(胶水:: glue("{cur_column()} _ avail")== 1〜.x,胶水:: glue("{cur_column()} _ avail")== 0〜as.numeric(NA)),.names ="{.col} _new")) 

但无济于事.有人可以澄清是否可以通过mutate(across ..语法吗?

解决方案

您可以通过 cur_column() get 来实现.

 库(dplyr)df%>%mutate(across(.cols = c(a,b),.fns =〜case_when(get(glue :: glue("{cur_column()} _ avail"))== 1〜.x,get(glue :: glue("{cur_column()} _ avail"))== 0〜as.numeric(NA)),.names ="{.col} _new"))#a a_avail b b_avail a_new b_new#< dbl>< dbl>< dbl>< dbl>< dbl>< dbl>#1 0 1 1 1 0 1#2 1 1 1 0 1不适用#3 0 1 1 0 0不适用#4 0 0 0 1 NA 0#5 0 0 0 0不适用不适用 

PS-我不确定这是否应该是您链接的帖子的答案.

This question though having three answers raised me doubts as I am mulling my head over the problem. Though I am aware that problem can be solved by other methods (and using purrr or apply group of functions especially), Yet I am not sure that can It be actually done through mutate(across(...? I am reproducing the problem for sake of clarity here. Note: I am not looking for its answer but only an answer to my doubt whether two sets of variables can actually be passed through mutate/across

There are two sets of variables (one without suffix and one set with suffix avail).

df <- tibble(a = c(0, 1, 0, 0, 0),
       a_avail = c(1, 1, 1, 0, 0),
       b = c(1, 1, 1, 0, 0),
       b_avail = c(1, 0, 0, 1, 0))
# A tibble: 5 x 4
      a a_avail     b b_avail
  <dbl>   <dbl> <dbl>   <dbl>
1     0       1     1       1
2     1       1     1       0
3     0       1     1       0
4     0       0     0       1
5     0       0     0       0

Now If we want to mutate one set of variables say (a and b) but by comparing these by another set in tandem. That is to say when column a is mutating it may use its corresponding variable a_avail and while b is mutating it is b_avail and so on upto n variables.

I have tried these codes apart from OP has

df %>% mutate(d = row_number()) %>%
  mutate(across(.cols = c(a_avail, b_avail),
                .fns = ~case_when(
                  .x == 1 ~ {str_replace(cur_column(), "_avail", "")[d]},
                  .x == 0 ~ NA_character_
                ),
                .names = "{.col}_new"))

OR

df %>% 
  mutate(across(.cols = c(a, b),
                .fns = ~case_when(
                  glue::glue("{cur_column()}_avail") == 1 ~ .x,
                  glue::glue("{cur_column()}_avail") == 0 ~ as.numeric(NA)
                ),
                .names = "{.col}_new"))

but to no avail. can someone clarify that whether it can be done through mutate(across.. syntax?

解决方案

You can do this with get with cur_column().

library(dplyr)

df %>% 
  mutate(across(.cols = c(a, b),
                .fns = ~case_when(
                  get(glue::glue("{cur_column()}_avail")) == 1 ~ .x,
                  get(glue::glue("{cur_column()}_avail")) == 0 ~ as.numeric(NA)
                ),
                .names = "{.col}_new"))

#      a a_avail     b b_avail a_new b_new
#  <dbl>   <dbl> <dbl>   <dbl> <dbl> <dbl>
#1     0       1     1       1     0     1
#2     1       1     1       0     1    NA
#3     0       1     1       0     0    NA
#4     0       0     0       1    NA     0
#5     0       0     0       0    NA    NA

PS - I am not sure if this should be an answer to the post that you linked.

这篇关于我们能否实际将两组多个变量传递给dplyr中的mutate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆