wesleysc352
wesleysc352

Reputation: 617

how to determine season dry or rainy in temporal analysis using R?

I have the data temporal of temperature, i would like determinate if date be to season dry or rainy.

In my coutry the season dry start in May up to October, and season rainy start in November up to April.

Would be possible create a column with this information in package dplyr ou other?

my data-frame in:

sample_station <-c('A','A','A','A','A','A','A','A','A','A','A','B','B','B','B','B','B','B','B','B','B','C','C','C','C','C','C','C','C','C','C','A','B','C','A','B','C')
Date_dmy <-c('01/01/2000','08/08/2000','16/03/2001','22/09/2001','01/06/2002','05/01/2002','26/01/2002','16/02/2002','09/03/2002','30/03/2002','20/04/2002','04/01/2000','11/08/2000','19/03/2001','25/09/2001','04/06/2002','08/01/2002','29/01/2002','19/02/2002','12/03/2002','13/09/2001','08/01/2000','15/08/2000','23/03/2001','29/09/2001','08/06/2002','12/01/2002','02/02/2002','23/02/2002','16/03/2002','06/04/2002','01/02/2000','01/02/2000','01/02/2000','02/11/2001','02/11/2001','02/11/2001')
Temperature <-c(17,20,24,19,17,19,23,26,19,19,21,15,23,18,22,22,23,18,19,26,21,22,23,27,19,19,21,23,24,25,26,29,30,21,25,24,23)

df<-data.frame(sample_station, Date_dmy, Temperature)

Upvotes: 2

Views: 153

Answers (2)

TarJae
TarJae

Reputation: 78927

Similar to akrun's solution but with ifelse:

library(dplyr)
library(lubridate)
df <- df %>%
  mutate(Month = month(dmy(Date_dmy)), 
         categ = ifelse(Month %in% 5:10,'dry','rainy'))

Output:

   sample_station   Date_dmy Temperature Month categ
1               A 01/01/2000          17     1 rainy
2               A 08/08/2000          20     8   dry
3               A 16/03/2001          24     3 rainy
4               A 22/09/2001          19     9   dry
5               A 01/06/2002          17     6   dry
6               A 05/01/2002          19     1 rainy
7               A 26/01/2002          23     1 rainy
8               A 16/02/2002          26     2 rainy
9               A 09/03/2002          19     3 rainy
10              A 30/03/2002          19     3 rainy
11              A 20/04/2002          21     4 rainy
12              B 04/01/2000          15     1 rainy
13              B 11/08/2000          23     8   dry
14              B 19/03/2001          18     3 rainy
15              B 25/09/2001          22     9   dry
16              B 04/06/2002          22     6   dry
17              B 08/01/2002          23     1 rainy
18              B 29/01/2002          18     1 rainy
19              B 19/02/2002          19     2 rainy
20              B 12/03/2002          26     3 rainy
21              B 13/09/2001          21     9   dry
22              C 08/01/2000          22     1 rainy
23              C 15/08/2000          23     8   dry
24              C 23/03/2001          27     3 rainy
25              C 29/09/2001          19     9   dry
26              C 08/06/2002          19     6   dry
27              C 12/01/2002          21     1 rainy
28              C 02/02/2002          23     2 rainy
29              C 23/02/2002          24     2 rainy
30              C 16/03/2002          25     3 rainy
31              C 06/04/2002          26     4 rainy
32              A 01/02/2000          29     2 rainy
33              B 01/02/2000          30     2 rainy
34              C 01/02/2000          21     2 rainy
35              A 02/11/2001          25    11 rainy
36              B 02/11/2001          24    11 rainy
37              C 02/11/2001          23    11 rainy

Upvotes: 2

akrun
akrun

Reputation: 887048

One option is to extract the month after converting to Date class, create a condition in case_when to return 'dry', 'rainy' based on the values of 'Month' column

library(dplyr)
library(lubridate)
df <- df %>%
  mutate(Month = month(dmy(Date_dmy)), 
     categ = case_when(Month %in% 5:10 ~ 'dry', TRUE ~ 'rainy'))

Upvotes: 2

Related Questions