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?
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))
# ^^^
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With