Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use mutate_at to change multiple column types

Tags:

r

dplyr

I'm trying to use dplyr to tidy a dataset. The columns I want to change have a character string that's really a double but with comma instead of a decimal point. So far I got this:

presupuesto_2016 <- read_csv( "http://datos.gob.ar/dataset/89f1a2dd-ad79-4211-87b4-44661d81ac0d/resource/84e23782-7d52-4724-a4ba-2f9621fa5f4e/download/presupuesto-2016.csv")

names(presupuesto_2016) <- str_replace(names(presupuesto_2016), "\uFEFF", "")

presupuesto_2016 %>%
  mutate_at(starts_with("monto_"),
            str_replace, pattern = ",", replacement = "\\.") %>% 
  mutate_at(starts_with("monto_"), funs(as.numeric))

But this manages to change every column to numeric. What am I doing wrong here?

like image 777
Luciano Selzer Avatar asked Sep 01 '16 19:09

Luciano Selzer


1 Answers

If you want to use mutate_at and column selection helper functions, they have to be wrapped in the vars function to work properly, take a look at ?mutate_at:

presupuesto_2016 %>%
  mutate_at(vars(starts_with("monto_")),
  #         ^^^ 
            str_replace, pattern = ",", replacement = "\\.") %>% 
  mutate_at(vars(starts_with("monto_")), funs(as.numeric))
  #         ^^^ 
like image 139
Psidom Avatar answered Sep 17 '22 15:09

Psidom