Format Date dd/mm with no year

I have a date column with data for the one month. The format is dd/mm with no year and is a 'fctr'. I know the data was from 2018, but not sure how to convert this, or if you can, using the as.date function. When I try to run this, I get an error:

Error in charToDate(x) : character string is not in a standard unambiguous format.

Appreciate any guidance on this.

My dates are:
01/07
02/07
03/07 etc

and I want to get them to a date format like this:
01/07/18

You need to create a valid date string by adding the year before converting it into a date type variable.

df <- data.frame(col = c("01/07", "02/07", "03/07"), stringsAsFactors = TRUE)

str(df)
#> 'data.frame':    3 obs. of  1 variable:
#>  $ col: Factor w/ 3 levels "01/07","02/07",..: 1 2 3

df$col <- as.Date(paste(df$col, "2018", sep = "/"), format = "%d/%m/%Y")

df
#>          col
#> 1 2018-07-01
#> 2 2018-07-02
#> 3 2018-07-03

Created on 2020-10-11 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.