Skip to content
Prev 393538 / 398503 Next

Date order question

On Wed, 4 Jan 2023, 21:29 Ebert,Timothy Aaron, <tebert at ufl.edu> wrote:

            
Put the year in front +/- apply as.Date() and you would be fine...

date<-c("2022-12-29","2022-12-30","2023-01-01") |> as.Date()

It may be that the source data doesn't have a year and the example given is
to show us dummy data.  You could 'automate' the addition along the lines
of:

require(tidyverse)
#if you have up to date Tidyverse this includes lubridate
current_date <- sys.Date()
current_month <- month(current_date)
current_year <- year(current_date)
date<-c("12-29","12-30","01-01")
PT <- c(.106,.130,.121)
data <- data.frame(date,PT)
data |>
  # separate the date into month and day column
  separate (date, c("Month", "Day"), sep="-") |>
  # add a year if month is > current month must be last year
 mutate (year = if_else(Month > current_month, current_year - 1,
current_year)) |>
  #rebuild the date
  unite (date, c("Year", "Month", "Day"), sep="-") |>
  mutate(date = as.Date(date)) -> data

If you don't want year on the axis of the graph, that should be dealt with
in ggplot not in the data carpentry
Message-ID: <CA+etgPnUSSxsaTsn3Dc6TAtLjY1uRhon_HXRp5x-TjdJG_ofAg@mail.gmail.com>
In-Reply-To: <BN6PR2201MB15538EF366D4F448311D73D0CFF59@BN6PR2201MB1553.namprd22.prod.outlook.com>