I´m trying to translate a column on a dataframe that has month numbers (1 for January, 2 for February and so on), so I want to translate those numbers to spanish month names. First I tried:
df$month <- month.name[df$month]
That works fine, but the outputs shows english names.
Then I tried using gsub:
num <- c(1,2,3,4,5,6,7,8,9,10,11,12)
meses <- c("Enero","Febrero","Marzo","Abril","Mayo","Junio","Julio",
"Agosto","Septiembre","Octubre","Noviembre","Diciembre")
gsub2 <- function(pattern, replacement, x, ...) {
for(i in 1:length(pattern))
x <- gsub(pattern[i], replacement[i], x, ...)
x
}
df$month <- gsub2(num, meses,df$month)
But with this code, my output is as follows:
"Enero" "Febrero" "Marzo" "Abril" "Mayo"
"Junio" "Julio" "Agosto" "Septiembre" "Enero0" "EneroEnero" "EneroFebrero"
I know what is happening, but I don't know how to fix it. Thanks in advance.
Edit:
df$month <- c(1, 2, 3, 4, 5, 6 ,7, 8, 9, 10, 11, 12)
Instead of using regex, this can be better solved with vectorization
unname(setNames(meses, num)[tabla$month])
#[1] "Abril" "Marzo" "Septiembre" "Julio" "Agosto" "Diciembre" "Abril" "Octubre" "Octubre" "Abril" "Agosto" "Mayo"
#[13] "Septiembre" "Septiembre" "Abril" "Noviembre" "Marzo" "Enero" "Julio" "Febrero"
and the corresponding tabla$month
is
tabla$month
#[1] 4 3 9 7 8 12 4 10 10 4 8 5 9 9 4 11 3 1 7 2
Regarding the use of regex
, probably, we need to add the start (^
) and end ($
) to avoid getting matches for 1
multiple times in 11
or 1
and 2
for 12
etc resulting in "EneroEnero"
or "EneroFebrero"
gsub2 <- function(pattern, replacement, x, ...) {
for(i in 1:length(pattern))
x<- gsub(paste0("^", pattern[i], "$"), replacement[i], x , ...)
x
}
gsub2(num, meses,tabla$month)
#[1] "Abril" "Marzo" "Septiembre" "Julio" "Agosto" "Diciembre" "Abril" "Octubre" "Octubre" "Abril" "Agosto" "Mayo"
#[13] "Septiembre" "Septiembre" "Abril" "Noviembre" "Marzo" "Enero" "Julio" "Febrero"
set.seed(24)
tabla <- data.frame(month = sample(1:12, 20, replace = TRUE))
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