combine month and day into one date column

r

Solution

Try this...

df <- data.frame( Month = sample(1:12 , 10 , repl = TRUE ) , Day = sample(1:30 , 10 , repl = TRUE ) )
df$Date <- as.Date( paste( df$Month , df$Day , sep = "." )  , format = "%m.%d" )

df
#      Month Day       Date
#   1      1   8 2013-01-08
#   2      1  17 2013-01-17
#   3      7  23 2013-07-23
#   4     11  21 2013-11-21
#   5      3  30 2013-03-30
#   6     12  15 2013-12-15
#   7      2  30       <NA>
#   8      7  10 2013-07-10
#   9      1  16 2013-01-16
#   10     8   1 2013-08-01

I got some NAs because I made up random dates, some of which don't exist, such as 30th Feb

Problem

Using R, I'd like to combine into one column (date) the month nb (month) and the day nb (day) contained in two different columns and use the created column in a date format. my data frame looks like this: ``` St Dep Month Day A 2 1 1 B 2 1 1 B 2 2 1 A 5 1 1 A 7 1 1 C 2 1 1 C 5 1 1 ``` And I want to add a column with something like "Jan-01".

Original source