I have been given a csv with a column called month as a char variable with the first three letters of the month. E.g.:
\"Jan\", \"Feb\",\"Mar\",...\"Dec\"
Use match
and the predefined vector month.abb
:
tst <- c("Jan","Mar","Dec")
match(tst,month.abb)
[1] 1 3 12
A couple of options using:
vec <- c("Jan","Dec","Jan","Apr")
are
> Months <- 1:12
> names(Months) <- month.abb
> unname(Months[vec])
[1] 1 12 1 4
and/or
> match(vec, month.abb)
[1] 1 12 1 4
Just adding to the existing answers and the comment in the question:
readr::parse_date("20/DEZEMBRO/18","%d/%B/%y",locale=locale("pt"))
Results date format "2018-12-20"
. locale("pt")
is for Portuguese, which is used in Brazil, can do "es"
for Spanish, "fr"
for French etc.
You can use the built-in vector month.abb
to check against when converting to a number, eg :
mm <- c("Jan","Dec","jan","Mar","Apr")
sapply(mm,function(x) grep(paste("(?i)",x,sep=""),month.abb))
Jan Dec jan Mar Apr
1 12 1 3 4
The grep construct takes care of differences in capitalization. If that's not needed,
match(mm,month.abb)
works just as fine.
If you also have a day and a year column, you can use any of the conversion functions, using the appropriate codes (see also ?strftime
)
eg
mm <- c("Jan","Dec","jan","Mar","Apr")
year <- c(1998,1998,1999,1999,1999)
day <- c(4,10,3,16,25)
dates <- paste(year,mm,day,sep="-")
strptime(dates,format="%Y-%b-%d")
[1] "1998-01-04" "1998-12-10" "1999-01-03" "1999-03-16" "1999-04-25"