简体   繁体   English

我们真的可以在 dplyr 中将两组多个变量传递给 mutate

[英]Can we actually pass two sets of multiple variables into mutate across in dplyr

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虽然我知道问题可以通过其他方法解决(特别是使用 purrr 或应用函数组),但我不确定它实际上可以通过mutate(across(...吗?我正在重现这个问题这里为了清楚起见。注意:我不是在寻找它的答案,而只是对我怀疑两组变量是否实际上可以通过 mutate/across 传递的答案

There are two sets of variables (one without suffix and one set with suffix avail).有两组变量(一组不带后缀,一组带后缀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.现在,如果我们想改变一组变量,比如说(a 和 b),但是通过将它们与另一组串联比较。 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.也就是说,当 a 列发生变异时,它可以使用其对应的变量 a_avail,而当 b 发生变异时,它是b_avail等等,最多 n 个变量。

I have tried these codes apart from OP has除了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::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?有人可以澄清一下是否可以通过 mutate(cross.. 语法来完成?

You can do this with get with cur_column() .您可以使用getcur_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. PS - 我不确定这是否应该是您链接的帖子的答案。

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

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