Conditional replacement of values in multiple columns

I want to replace values for multiple columns to NA based on the values in the other columns.
I end up with the following code, but I can't figure out how to refer to the original value from the column (if it shouldn't be replaced). In the example below, I want to replace values of displ, cty, why to NA if cyl equal 4.

suppressMessages(library(tidyverse))
mpg %>%
  mutate(across(.cols = c(displ, cty, hwy),
                .fns = case_when(cyl == 4 ~ as.numeric(NA),
                                 TRUE ~ .x)))
#> Error: Problem with `mutate()` input `..1`.
#> x object '.x' not found
#> i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.

Curiously, when I run the same code on a different machine, the error is different

suppressMessages(library(tidyverse))
mpg %>%
  mutate(across(.cols = c(displ, cty, hwy),
                .fns = case_when(cyl == 4 ~ as.numeric(NA),
                                 TRUE ~ .x)))
#> Error: Problem with `mutate()` input `..1`.
#> x object 'cyl' not found
#> i Input `..1` is `(function (.cols = everything(), .fns = NULL, ..., .names = NULL) ...`.

You've basically got it. You just need to add a tilde ~ before the case_when function call to create an anonymous function (which you must do if you want to input the arguments into case_when).

I've also put the .x within an as.numericfunction call, since displ is a numeric vector, but cty and hwy are integer vectors.

Here's your code with those minor changes.

suppressMessages(library(tidyverse))
mpg %>% 
  mutate(across(.cols = c(displ, cty, hwy),
                .fns = ~case_when(cyl == 4L ~ as.numeric(NA),
                                  TRUE ~ as.numeric(.x))))
#> # A tibble: 234 x 11
#>    manufacturer model    displ  year   cyl trans   drv     cty   hwy fl    class
#>    <chr>        <chr>    <dbl> <int> <int> <chr>   <chr> <dbl> <dbl> <chr> <chr>
#>  1 audi         a4        NA    1999     4 auto(l… f        NA    NA p     comp…
#>  2 audi         a4        NA    1999     4 manual… f        NA    NA p     comp…
#>  3 audi         a4        NA    2008     4 manual… f        NA    NA p     comp…
#>  4 audi         a4        NA    2008     4 auto(a… f        NA    NA p     comp…
#>  5 audi         a4         2.8  1999     6 auto(l… f        16    26 p     comp…
#>  6 audi         a4         2.8  1999     6 manual… f        18    26 p     comp…
#>  7 audi         a4         3.1  2008     6 auto(a… f        18    27 p     comp…
#>  8 audi         a4 quat…  NA    1999     4 manual… 4        NA    NA p     comp…
#>  9 audi         a4 quat…  NA    1999     4 auto(l… 4        NA    NA p     comp…
#> 10 audi         a4 quat…  NA    2008     4 manual… 4        NA    NA p     comp…
#> # … with 224 more rows

Created on 2021-04-15 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.