(I actually came up with a solution but that didn't satisfy my desire for simplicity and intuitiveness, therefore here I state my question and solution while waiting for a nice and neat solution.)
I have a data with one column being Year
and the other being Month
, while the month is in the format of string:
Country Month Year Type
<fct> <chr> <dbl> <fct>
1 Argentina June 1975 Currency
2 Argentina February 1981 Currency
3 Argentina July 1982 Currency
I am trying to combine the Month and Year column to a single column Date
, which is in the format of date
.
First Try
My first try was to use mapply
, with the help of lubridate
and a little function of my that transforms month
from string to int.
months = c("January", "February", "March", "April", 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December')
month_num = c(1:12)
names(month_num) = months
crisis$Date = mapply(function(y, m){
m = month_num[m]
d = make_date(y,m)
return(d)
},crisis$Year, crisis$Month)
However this didn't turn out to be what I want:
Country Month Year Type Date
<fct> <chr> <dbl> <fct> <list>
1 Argentina June 1975 Currency <date [1]>
2 Argentina February 1981 Currency <date [1]>
3 Argentina July 1982 Currency <date [1]>
4 Argentina September 1986 Currency <date [1]>
, as the Date
column is list format.
Some Googling
With some help from this post and some manipulation on unlisting it and turning it back to date object, I managed to get the result I want:
crisis$Date = as_date(unlist(mapply(function(y, m){
m = month_num[m]
d = make_date(y,m)
return(d)
},crisis$Year, crisis$Month, SIMPLIFY = FALSE)))
The result is
Country Month Year Type Date
<fct> <chr> <dbl> <fct> <date>
1 Argentina June 1975 Currency 1975-06-01
2 Argentina February 1981 Currency 1981-02-01
3 Argentina July 1982 Currency 1982-07-01
4 Argentina September 1986 Currency 1986-09-01
This is so far fine to deal with, but I believe there are better solutions.