I have a dataframe with a list of consecutive measurements of temperature over a period of time. I would like to split these up into separate dataframes based on when the temperature is consecutively above zero. For example:
Date | Time | Temp |
---|---|---|
2022-05-20 | 4:16 | 32 |
2022-05-20 | 4:17 | 16 |
2022-05-20 | 4:18 | 0 |
2022-05-20 | 4:19 | 7 |
2022-05-20 | 4:34 | 75 |
2022-05-20 | 4:39 | 5 |
2022-05-20 | 4:48 | 0 |
2022-05-20 | 4:49 | 0 |
2022-05-20 | 4:59 | 83 |
The desired output would be:
Date | Time | Temp |
---|---|---|
2022-05-20 | 4:16 | 32 |
2022-05-20 | 4:17 | 16 |
and
Date | Time | Temp |
---|---|---|
2022-05-20 | 4:19 | 7 |
2022-05-20 | 4:34 | 75 |
2022-05-20 | 4:39 | 5 |
and
Date | Time | Temp |
---|---|---|
2022-05-20 | 4:59 | 83 |
A base R approach would be to use split
with cumsum
:
split(df[df$Temp != 0, ],
cumsum(df$Temp == 0)[df$Temp != 0])
Or a much more readable format suggested by @thelatemail
sel <- df$Temp == 0
split(df[!sel,], cumsum(sel)[!sel])
Output for both:
# $`0`
# Date Time Temp
# 1 2022-05-20 4:16 32
# 2 2022-05-20 4:17 16
#
# $`1`
# Date Time Temp
# 4 2022-05-20 4:19 7
# 5 2022-05-20 4:34 75
# 6 2022-05-20 4:39 5
#
# $`3`
# Date Time Temp
# 9 2022-05-20 4:59 83
(Using these data)
df <- read.table(text = "Date Time Temp
2022-05-20 4:16 32
2022-05-20 4:17 16
2022-05-20 4:18 0
2022-05-20 4:19 7
2022-05-20 4:34 75
2022-05-20 4:39 5
2022-05-20 4:48 0
2022-05-20 4:49 0
2022-05-20 4:59 83", header = TRUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With