R Counting related dates in an XTS (or data.frame) object

638 Views Asked by At

in my studies I am analyzing historical wind speeds of meteorological stations (if the trend is increasing or decreasing etc.). Now i have one value for each day. I want to count the days, which are related to each other (for example 1958-03-18, 19, 20...). for every year i want to have a value how many (storm)days occured with more than 3 days in a row.

example:

1982-01-30  41.04
1982-02-02  45.72
1982-02-03  46.8
1982-02-04  41.04
1982-02-12  39.24
1982-02-17  53.28
1982-02-18  49.68
1982-02-19  40.32
1982-03-01  46.08

In February 1982 2 times is the situation (2,3,4) and (17,18,19).

Does anyone know how to count this and put it in a new table for further analyzing / plotting?

1982 23
1983 7
1984 11
.
.
.

At least just counting all days per year would help me,too.

Kind Regards Sascha

1

There are 1 best solutions below

1
jules On

Below is a simple way to identify storm days (wind > 39 according to your comment) and dates with three successive storm days.

#create example data
fakedates <- as.Date("1982-01-01")+(1:730)
fakewind <- sample(1:80, length(fakedates), replace = TRUE)
dateyear <- format(fakedates, "%Y")

df <- data.frame("dates"=fakedates, "maxwind"=fakewind, "year"=dateyear)

#identify 'storm' days where max wind is >39
df$storm <- df$maxwind > 39  #identify storm days

# ensure original data is in chronological order, otherwise you would need to sort first
# e.g.  if(is.unsorted(original$dates), sort(original$dates, .... etc)

#identify 3-day storms - undetermined for last two days in the record
df$storm3 <-
  c(df$storm[1:(length(df$storm) - 2)] &
      df$storm[2:(length(df$storm) - 1)] &
      df$storm[3:(length(df$storm))], "unknown", "unknown")

#select all storm day records
dfs <- df[df$storm == TRUE, c(1:3)]

#select only dates of 3-day storms
dfs3 <- df[df$storm3== TRUE,(1:3)]

Your output would look something like this:

> head(dfs3)
        dates maxwind year
12 1982-01-13      57 1982
13 1982-01-14      47 1982
33 1982-02-03      65 1982
49 1982-02-19      79 1982
61 1982-03-03      49 1982
68 1982-03-10      55 1982

You can summarise by year and do whatever other analyses you require, either with all storm days (dfs) or with three-day storm days only (dfs3).

When you are more famiiar with R, you could tackle the task in various other ways e.g. with package dplyr