Hi, I would like to group my hourly data in 2018 into 4 seasons and I have tried to do it but it is not successful, so do you guys know how to do it?
TMData$DATE <- case_when(
between(TMData$DATE, as.Date("2018-12-31"), as.Date("2018-02-28")) ~ "Winter",
between(TMData$DATE, as.Date("2018-03-01"), as.Date("2018-05-31")) ~ "Spring",
between(TMData$DATE, as.Date("2018-06-01"), as.Date("2018-08-31")) ~ "Summer",
between(TMData$DATE, as.Date("2018-09-01"), as.Date("2018-11-30")) ~ "Autumn",
TRUE ~ "Fall"
)
Error in case_when()
:
! Failed to evaluate the left-hand side of formula 1.
Caused by error in between()
:
! Can't combine x
and left
.
Run rlang::last_trace()
to see where the error occurred.
Hi,
I think between
doesn’t handle dates, but numeric values:
You could try Lubridate
Functions to work with date-times and time-spans: fast and
user friendly parsing of date-time data, extraction and updating of
components of a date-time (years, months, days, hours, minutes, and
seconds), algebraic manipulation on...
Then you can make an interval and use %within%
Also, I would check your Winter dates.
between()
does work with dates, e.g.
dplyr::between(as.Date("2024-02-15"), as.Date("2024-02-14"), as.Date("2024-02-16"))
I suspect that TMData$DATE
may not be defined as a date, but it's impossible to know without a reproducible example:
A minimal reproducible example consists of the following items:
A minimal dataset, necessary to reproduce the issue
The minimal runnable code necessary to reproduce the issue, which can be run
on the given dataset, and including the necessary information on the used packages.
Let's quickly go over each one of these with examples:
Minimal Dataset (Sample Data)
You need to provide a data frame that is small enough to be (reasonably) pasted on a post, but big enough to reproduce your issue.
Let's say, as an example, that you are working with the iris data frame
head(iris)
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.…
1 Like
system
Closed
March 7, 2024, 10:17am
4
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.