Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split up dataframe into list of dataframes based on breaks in one column

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
like image 544
Nadka Avatar asked Sep 16 '25 23:09

Nadka


1 Answers

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)
like image 86
jpsmith Avatar answered Sep 19 '25 16:09

jpsmith