Date Managment with Lubridate

Hi, how to, from a yy-mm-dd date column, can a mm-dd column be created ? thanks

Here is some code that takes a column that is of the type character and has the form yy-mm-dd, turns it into a date and then makes a new column of characters that have the form mm-dd. I included calls to str() to show how the process is working. It is not possible, as far as I know, to have a Date consisting of only the month and the day.

What are you trying to accomplish with the mm-dd column?

DF <- data.frame(Date = c("20-04-03", "20-05-01", "20-06-13"))
str(DF)
#> 'data.frame':    3 obs. of  1 variable:
#>  $ Date: chr  "20-04-03" "20-05-01" "20-06-13"
library(lubridate)                 
#> 
#> Attaching package: 'lubridate'
#> The following objects are masked from 'package:base':
#> 
#>     date, intersect, setdiff, union
DF$Date <- ymd(DF$Date)
str(DF)
#> 'data.frame':    3 obs. of  1 variable:
#>  $ Date: Date, format: "2020-04-03" "2020-05-01" ...
DF$NewDate <- format(DF$Date, "%m-%d")
str(DF)
#> 'data.frame':    3 obs. of  2 variables:
#>  $ Date   : Date, format: "2020-04-03" "2020-05-01" ...
#>  $ NewDate: chr  "04-03" "05-01" "06-13"

Created on 2020-08-26 by the reprex package (v0.3.0)

1 Like

Hi FJCC, thanks for the answer...still the """""F$NewDate <- format(DF$Date, "%m-%d")"""" code gives NewDate a character format. How to get it formatted as date...I need to make date operations by month-date ....regards, A.

There is no such thing as a yearless month-date... thinngs like the number of days in february are dependent on year..
That said you could set an arbitrary year to calculate and then omit the year when presenting results.
However, for best support can you give examples of the sorts of date operations you want to do ?

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.