Using "mutate_each" from the dplyr-library

I'd like to use the same function on different variables. However, I don't know how exactly this can be done with dplyr-library in R.

I'd like to change the values of 6 variable to NA if they have the value "-8". This is what I came up with (it works but it looks clumsy):

bfs_data %>%
  
  select(musul_sty_01,
         musul_sty_02,
         musul_sty_03,
         musul_sty_04,
         musul_sty_05,
         musul_sty_06) %>%
  mutate(
    musul_sty_01 = na_if(musul_sty_01,"-8"),
    musul_sty_02 = na_if(musul_sty_02,"-8"),
    musul_sty_03 = na_if(musul_sty_03,"-8"),
    musul_sty_04 = na_if(musul_sty_04,"-8"),
    musul_sty_05 = na_if(musul_sty_05,"-8"),
    musul_sty_06 = na_if(musul_sty_06,"-8")
  )

So I tried to use mutate_each like this:

sty_muslim <- bfs_data %>%
  select(musul_sty_01,
         musul_sty_02,
         musul_sty_03,
         musul_sty_04,
         musul_sty_05,
         musul_sty_06) %>%
  mutate_each(
    funs(na_if("-8")),
    musul_sty_01,
    musul_sty_02,
    musul_sty_03,
    musul_sty_04,
    musul_sty_05,
    musul_sty_06
  )

It does not work and I dont know why. I get this error:

check_length(y, x, fmt_args("y"), glue("same as {fmt_args(~x)}")) : Argument "x" fehlt (ohne Standardwert)

I have never used mutate_each before so I very appreciate your help!



Read more here: https://stackoverflow.com/questions/64403392/using-mutate-each-from-the-dplyr-library

Content Attribution

This content was originally published by Linus at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: