Reputation: 330
Hi I wanna split a data frame by a set of specific dates (not according to "month" or "week").
Here is the sample code
id <- c(1:6)
ts <- as.Date(c("2018-06-01","2018-06-25","2018-06-03","2018-05-06","2018-04-30","2018-06-13"))
df <- data.frame(id,ts)
split(df,cut(ts,breaks = c("2018-05-01","2018-05-20","2018-06-14")))
But it says invalid specification of breaks.
I am wondering how can I specify breaks in this case? I also tried cut.dates and tried different formats, e.g. "dd/mm/yy".
Many thanks in advance!
Upvotes: 2
Views: 53
Reputation: 50728
Do you mean this?
split(df, cut(
df$ts,
breaks = as.Date(c("2018-04-30", "2018-05-01","2018-05-20","2018-06-14", "2018-06-30"))))
#$`2018-04-30`
# id ts
#5 5 2018-04-30
#
#$`2018-05-01`
# id ts
#4 4 2018-05-06
#
#$`2018-05-20`
# id ts
#1 1 2018-06-01
#3 3 2018-06-03
#6 6 2018-06-13
#
#$`2018-06-14`
# id ts
#2 2 2018-06-25
You need to specify breaks
as Date
objects when using cut
with Date
s. Notice also that I added a lower and upper break point because the break
points you gave in your example did not cover the range of Date
s in df$ts
.
Upvotes: 1