Luciano Selzer Luciano Selzer - 10 months ago 88
R Question

Use mutate_at to change multiple column types

I'm trying to use

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( "")

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

presupuesto_2016 %>%
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?


Why not just do:

URL <- ""
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.