Reputation: 1123
I have an irregularly sampled time series. I would like to partition it to segments where the time difference between consecutive entries is (say) a week. For example:
day X
1 4
3 2
7 4
8 9
10 2
12 4
14 3
15 9
17 7
19 3
26 9
would be partitioned to:
day X
1 4
8 9
15 9
day X
3 2
10 2
17 7
day X
12 4
19 3
26 9
day X
7 4
14 3
I imagine I can brute force it with loops but I'm sure there are better ways since this seems like a fairly common task. Bonus points - instead of "week" use a range of days (say 6-8 days).
Upvotes: 1
Views: 98
Reputation: 887541
We can use split
to return a list
of data.frame
s
split(df, df$day%%7)
#$`0`
# day X
#3 7 4
#7 14 3
#$`1`
# day X
#1 1 4
#4 8 9
#8 15 9
#$`3`
# day X
#2 3 2
#5 10 2
#9 17 7
#$`5`
# day X
#6 12 4
#10 19 3
#11 26 9
Upvotes: 2