简体   繁体   中英

How to use mutate_at or mutate_if at the same time to do multiple action on data

I would like to apply 3 functions using one code on the same variables in my data. I have a data set and there are certain columns in my data and i want to apply these function to all of them. 1- make them all factor data 2- replace spaces in the columns with missing(convert space values to missing) 3- give missing value an explicit factor level using fct_explicit_na

i have done this in separate code lines but i want to merge all of them using dplyr mutate function. I tried the following but didnt work

cols <- c("id12", "id13", "id14", "id15")

data_new <- data_old %>%                 
  mutate_if(cols=="", NA) %>%            # replace space with NA for cols
  mutate_at(cols, factor) %>%            # then turn them into factors
  mutate_at(cols, fct_explicit_na)       # give NAs explicit factor level
  )

I get the error: Error in tbl_if_vars(.tbl, .p, .env, ..., .include_group_vars = .include_group_vars) : length(.p) == length(tibble_vars) is not TRUE

The mutate_if step is not doing what the OP intend to do. Instead, we can do this in a single step with

library(dplyr)
data_old %>%
       mutate_at(vars(cols), ~ na_if(., "") %>%
                                 factor %>%
                                 fct_explicit_na)

Why the OP's code didn't work?

Using a reproducible example, below code converts columns that are factor to character class

iris1 <- iris %>% 
            mutate_if(is.factor, as.character) %>%
            mutate(Species = replace(Species, c(1, 3, 5), ""))

Now, if we do

iris1 %>%
      mutate_if("Species" == "", NA)

it is comparing two strings instead of checking the column values. Also, mutate_if should return a logical vector of length 1 for selecting that column.

Instead, if we use

iris1 %>%
     mutate_if(~ any(. == ""), ~ na_if(., "")) %>%
     head

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