Last active
August 10, 2020 15:52
-
-
Save naranjja/3308f3b9465084584500ba0653efb74d to your computer and use it in GitHub Desktop.
Cómo parsear meses en R sin día ni año
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(readr) # para leer fechas en español | |
library(dplyr) # para usar mutate y pipes | |
# asumiendo un data.frame cualquiera con una columna "x2" con | |
# los nombres de los meses, sin día y sin año | |
df = data.frame( | |
x1 = c('1', '2', '3'), | |
x2 = c("ENERO", "DICIEMBRE", "AGOSTO") | |
) | |
# hacer un mutate que le ponga día y año (para que sea una fecha válida) | |
# en el formato "día mes año", separado por espacios, y luego convertir | |
# este texto separado por espacios a un objeto Date usando parse_date | |
df = df %>% | |
mutate(x2 = paste0("1 ", x2, " 2020")) %>% | |
mutate(x2 = parse_date(x2, format = "%d %B %Y", locale = locale("es"))) | |
# finalmente imprimir el data.frame | |
print(df) | |
# x1 x2 | |
# 1 2020-01-01 | |
# 2 2020-12-01 | |
# 3 2020-08-01 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment