Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data wrangling with R

Tags:

r

dplyr

tidyverse

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?

like image 971
Julapalli Harish Avatar asked Nov 24 '25 06:11

Julapalli Harish


1 Answers

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

like image 191
Thomas Mock Avatar answered Nov 25 '25 19:11

Thomas Mock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!