1

I have a data set of 365 days that contain one value for temperature respectively. I would like to analyse the period of days, that contain positive temperatures and periods that contain negative temperatures. So I want to count the days of a period until the temperature breaks 0. I already set up a column that tracks the algebraic sign, but now I want to add another column that tracks the days that are positive and the days that are negative, so that I can group_by every period and to calculate the mean of Consumption, min, max, etc. I am currently not sure how to draw the result in a plot, but maybe you have an idea..

         Time  Consumption STABW_hour   Min    Max     mean DeltaDaymean  Sign
       <dttm>        <dbl>      <dbl> <dbl>  <dbl>    <dbl>        <dbl> <dbl>
1  2014-01-01       1053.5   76.56046   964 1597.5 1159.804   -106.30411    -1
2  2014-01-02       1197.5   76.56046   964 1597.5 1159.804     37.69589     1
3  2014-01-03       1245.5   76.56046   964 1597.5 1159.804     85.69589     1
4  2014-01-04       1147.5   76.56046   964 1597.5 1159.804    -12.30411    -1
5  2014-01-05       1194.0   76.56046   964 1597.5 1159.804     34.19589     1
6  2014-01-06       1171.5   76.56046   964 1597.5 1159.804     11.69589     1
7  2014-01-07       1166.5   76.56046   964 1597.5 1159.804      6.69589     1
8  2014-01-08       1135.0   76.56046   964 1597.5 1159.804    -24.80411    -1
9  2014-01-09       1168.0   76.56046   964 1597.5 1159.804      8.19589     1
10 2014-01-10       1181.5   76.56046   964 1597.5 1159.804     21.69589     1

The result should be something like this, I would then be able to group_by the data by the new column..

         Time  Consumption STABW_hour   Min    Max     mean DeltaDaymean  Sign Period
       <dttm>        <dbl>      <dbl> <dbl>  <dbl>    <dbl>        <dbl> <dbl>  <dbl>
1  2014-01-01       1053.5   76.56046   964 1597.5 1159.804   -106.30411    -1      1
2  2014-01-02       1197.5   76.56046   964 1597.5 1159.804     37.69589     1      2
3  2014-01-03       1245.5   76.56046   964 1597.5 1159.804     85.69589     1      2
4  2014-01-04       1147.5   76.56046   964 1597.5 1159.804    -12.30411    -1      3
5  2014-01-05       1194.0   76.56046   964 1597.5 1159.804     34.19589     1      4
6  2014-01-06       1171.5   76.56046   964 1597.5 1159.804     11.69589     1      4
7  2014-01-07       1166.5   76.56046   964 1597.5 1159.804      6.69589     1      4
8  2014-01-08       1135.0   76.56046   964 1597.5 1159.804    -24.80411    -1      5
9  2014-01-09       1168.0   76.56046   964 1597.5 1159.804      8.19589     1      6
10 2014-01-10       1181.5   76.56046   964 1597.5 1159.804     21.69589     1      6

Thank you very much!

Erik Steiner
  • 581
  • 1
  • 5
  • 18

1 Answers1

0

Using dplyr, we can do this with

library(dplyr)
df1 %>% 
   mutate(Period = inverse.rle(within.list(rle(Sign), values <- seq_along(values))))

Or we can use rleid from data.table

library(data.table)
setDT(df1)[, Period := rleid(Sign)]
akrun
  • 874,273
  • 37
  • 540
  • 662