3

I have a long list of variables that I want to convert to factors. So, I did this:

factor_df <- original_df %>%
                  select(PhoneService:PaymentMethod) %>%
                  map(as.factor)

I get all the variables from PhoneService to PaymentMethod as factors.
How do I make this factor_df to attach to original_df again, so that the original_df has all the appropriate data types?

cmaher
  • 5,100
  • 1
  • 22
  • 34

1 Answers1

3
original_df %>%
    mutate_at(vars(PhoneService:PaymentMethod), as.factor)

The dplyr::mutate_at() function allows you to specify which variables or vars() you wish to apply a single function or multiple functions with funs().

Source: https://dplyr.tidyverse.org/reference/summarise_all.html

Thomas Mock
  • 206
  • 2
  • 5