My data takes this format:
library(tidyverse)
df <- mtcars
df <- df %>% mutate(vs_doubled = vs * 2) %>% select(mpg, cyl, vs, am, vs_doubled)
head(df)
#> mpg cyl vs am vs_doubled
#> 1 21.0 6 0 1 0
#> 2 21.0 6 0 1 0
#> 3 22.8 4 1 1 2
#> 4 21.4 6 1 0 2
#> 5 18.7 8 0 0 0
#> 6 18.1 6 1 0 2
I'm trying to use mutate_at
and na_if
to set 0 values as NA--but only for specific columns ("vs" and "am"). I would like to leave the column "vs_doubled" with zeros in it.
I haven't quite got it right, because the following line doesn't work:
df <- df %>% mutate_at(.vars = c("vs", "am"), .funs = na_if(y = 0))