[R] Date order question
CALUM POLWART
po|c1410 @end|ng |rom gm@||@com
Wed Jan 4 23:51:02 CET 2023
On Wed, 4 Jan 2023, 21:29 Ebert,Timothy Aaron, <tebert using ufl.edu> wrote:
>
> As you are plotting strings, you could put a space character in front of
> the December dates so that they are first.
> date<-c(" 12-29"," 12-30","01-01")
> That fixes the problem in this example. You can order all the dates by
> putting more spaces in front of earlier years. That will get messy.
>
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
[[alternative HTML version deleted]]
More information about the R-help
mailing list