与两套变量一起使用dplyr :: [英] Using dplyr::across with two sets of variables

查看:44
本文介绍了与两套变量一起使用dplyr ::的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组变量,例如变量 a 和变量 a_avail .我正在尝试根据 a_avail 的值更改 a 的值,并且想知道是否可以使用 across .

I have two sets of variables, for example variable a and variable a_avail. I am trying to change the value of a based on the value of a_avail and am wondering if this can be done using across with glue.

这是我尝试过的.没有错误产生,但是胶水似乎没有拾取 .x_avail 的值,因为所有返回的值都是NA:

Here is what I have tried. No error is produced, but the glue does not appear to be picking up the values of .x_avail since all returned values are NA:

library(tidyverse)

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

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

df2
#> # A tibble: 5 x 6
#>       a a_avail     b b_avail a_new b_new
#>   <dbl>   <dbl> <dbl>   <dbl> <dbl> <dbl>
#> 1     0       1     1       1    NA    NA
#> 2     1       1     1       0    NA    NA
#> 3     0       1     1       0    NA    NA
#> 4     0       0     0       1    NA    NA
#> 5     0       0     0       0    NA    NA

reprex软件包(v0.3.0)创建于2021-02-12 sup>

Created on 2021-02-12 by the reprex package (v0.3.0)

推荐答案

Ronak Shah在其答案中对相关的<一个href ="https://stackoverflow.com/q/67015561/2884859">问题提出了一种绝妙的方法,我将在下面复制.

Ronak Shah in his answer to a related question has suggested a fantastic approach, which I am reproducing below.

实际上有两件事

  • mutate(across .. )内部使用列/变量名称代替值 cur_column(),应与.或<代码> .x .
  • get()还可与 glue 一起使用,以便R将其识别为变量.
  • Inside mutate(across.. to use column/variable name instead of value cur_column() should be used as against . or .x.
  • get() may also be used alongwith glue so that R recognises it as a variable.

执行此操作

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 ~ NA_real_
                ),
                .names = "{.col}_new"))

# A tibble: 5 x 6
      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

这篇关于与两套变量一起使用dplyr ::的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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