Search code examples
rif-statementtime-serieslogical-operatorslogical-or

How to find the first row (date) of the series of 5 or more rows (date) with below 0 value


I have average temperature data with date below. I wanted to find the date which is the beginning of consistent below or above 0 Celsius in series of at least 5 days.

  date_short mean.temp
1 2018-05-18  17.54
2 2018-05-19  19.45
3 2018-05-20  22.31
4 2018-05-21  13.26
5 2018-05-22  10.29
6 2018-05-23  15.06

I have used following scripts and found out how many days are below 0 and what rows meet the criteria of below 0 temperature. It shows that there are total of 147 days with below 0 degree temperature and in which row the below 0 temperature observed. From that I can see 161st date is the first day with below 0 temperature, but it is not what I wanted, because it is not the first date of the series of at least 5 days with below or above 0 degree. Instead I want R to return 170th day as it is the beginning of the series of at least 5 days with below or above 0 degree.

length(which(d.mean$mean.temp <= 0))
[1] 147
which(d.mean$mean.temp <= 0)
  [1] 161 162 166 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
 [30] 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
 [59] 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
 [88] 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
[117] 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 307 308 309 310 313 314 315 316 317
[146] 318 324

How can I do it in R. I can do it manually, but I have to find such date for many columns. In excel, the function would be something like below.

IF(B2<0, IF(B3<0, IF(B4<0, IF(B5<0, IF(B6<0,A2,""),""),""),""),"")

Thanks in advance


Solution

  • One solution with tidyversere and zoo would be the following. You can use rollapply to find 5 temperatures less than zero, setting those as TRUE. As a rolling window, it will flag those dates that are followed by four additional (consecutive) dates with temperatures below zero. To filter out dates where these series take place, look at transition from FALSE to TRUE.

    Edit: If you have multiple columns of temperatures, and want to apply this approach to each column of temperatures, you can use pivot_longer and group_by first. Example now includes 3 columns of temperatures.

    set.seed(126)
    
    library(tidyverse)
    library(zoo)
    
    df %>%
      pivot_longer(cols = -date, names_to = "temp", values_to = "value") %>%
      group_by(temp) %>%
      mutate(start = rollapply(value < 0, width = 5, all, align = "left", fill = FALSE)) %>%
      dplyr::filter(start & !lag(start, default = FALSE)) %>%
      dplyr::select(date, temp) %>%
      arrange(temp, date)
    

    Output

    # A tibble: 7 x 2
    # Groups:   temp [3]
      date       temp  
      <date>     <chr> 
    1 2020-01-10 temp_A
    2 2020-01-16 temp_A
    3 2020-01-22 temp_A
    4 2020-01-05 temp_B
    5 2020-01-22 temp_B
    6 2020-01-01 temp_C
    7 2020-01-23 temp_C
    

    Data

    df <- data.frame(
      date = seq(as.Date("2020/01/01"), as.Date("2020/02/01"), "days"),
      temp_A = sample(c(-10:2), 32, replace = TRUE),
      temp_B = sample(c(-10:2), 32, replace = TRUE),
      temp_C = sample(c(-10:2), 32, replace = TRUE)
    )
    
             date temp_A temp_B temp_C
    1  2020-01-01     -9     -8     -6
    2  2020-01-02     -1      1     -9
    3  2020-01-03     -6     -7     -4
    4  2020-01-04      0      1     -9
    5  2020-01-05      2     -8     -3
    6  2020-01-06     -4     -3      0
    7  2020-01-07     -1     -3      1
    8  2020-01-08      2     -3      0
    9  2020-01-09      1     -6      1
    10 2020-01-10     -1     -7     -1
    11 2020-01-11     -2     -4     -6
    12 2020-01-12     -8     -2      1
    13 2020-01-13     -7      1     -5
    14 2020-01-14     -3     -2     -7
    15 2020-01-15      0      0     -8
    16 2020-01-16     -1     -4    -10
    17 2020-01-17     -4     -1      2
    18 2020-01-18     -6      1     -9
    19 2020-01-19     -5     -7     -5
    20 2020-01-20     -4     -6      0
    21 2020-01-21      2      0     -6
    22 2020-01-22     -1     -3      0
    23 2020-01-23     -4     -7     -3
    24 2020-01-24     -2     -7     -5
    25 2020-01-25    -10     -1    -10
    26 2020-01-26     -5     -6     -6
    27 2020-01-27     -3    -10     -1
    28 2020-01-28     -8     -5      1
    29 2020-01-29      0      1     -2
    30 2020-01-30      2     -9     -6
    31 2020-01-31    -10     -4     -1
    32 2020-02-01      2    -10     -4