Luciano Selzer Luciano Selzer - 3 months ago 21
R Question

Use mutate_at to change multiple column types

I'm trying to use

dplyr
to tidy a dataset. The coloumns 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?

Answer

Why not just do:

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

Also, I'd suggest doing:

fil <- basename(URL)
if (!file.exists(fil)) download.file(URL, fil)
presupuesto_2016 <- read_csv(fil, locale=locale(decimal_mark=","))

to save on your and that site's bandwidth, speed up future processing and ensure reproducibility in the event that site goes offline or you do.

Comments