简体   繁体   中英

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!

With the new version of dplyr you can use across() like this (Not tested as no data was shared):

library(dplyr)
#Code
newdata <- bfs_data %>% mutate(across(musul_sty_01:musul_sty_06,~ifelse(.=='-8',NA,.)))

To do this in base R, you can select the columns that you want to change using column names, numbers or pattern in their name. From the post it seems you are trying to change -8 to NA for columns that have 'musul_sty_' in it. We can use grep to select columns here.

For example, if your dataframe is :

bfs_data <- data.frame(a = 1:4, musul_sty_01 = c(-8, 2, 3, 4), 
                       musul_sty_02 = c(-2, 3, -8, 2))
bfs_data
#  a musul_sty_01 musul_sty_02
#1 1           -8           -2
#2 2            2            3
#3 3            3           -8
#4 4            4            2

With grep we select the column and then replace the -8 value with NA .

cols <- grep('musul_sty_', names(bfs_data))
bfs_data[cols][bfs_data[cols] == -8] <- NA

#  a musul_sty_01 musul_sty_02
#1 1           NA           -2
#2 2            2            3
#3 3            3           NA
#4 4            4            2

We can use data.table methods

library(data.table)
setDT(bfs_data)[, lapply(.SD, function(x) 
     fifelse( x == -8, NA_real_, x)), .SDcols = patterns('musul_sty_')]
#   musul_sty_01 musul_sty_02
#1:           NA           -2
#2:            2            3
#3:            3           NA
#4:            4            2

data

bfs_data <- data.frame(a = 1:4, musul_sty_01 = c(-8, 2, 3, 4), 
                   musul_sty_02 = c(-2, 3, -8, 2))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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