You could also use the parse_date_time
function from the lubridate
package:
library(lubridate)
day<-"31/08/2011"
as.Date(parse_date_time(day,"dmy"))
[1] "2011-08-31"
parse_date_time
returns a POSIXct object, so we use as.Date
to get a date object. The first argument of parse_date_time
specifies a date vector, the second argument specifies the order in which your format occurs. The orders
argument makes parse_date_time
very flexible.