Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need to count the number of times a threshold value is met (or exceeded) per year (using R)

Tags:

r

count

subset

I am working with several temperature datasets and trying to pull out when the temperature meets or exceeds a threshold value. Ideally, I want to know how many times (count) that value is met/ exceeded each year for ~100 yrs of data AND when (what date) that value is first exceeded and last exceeded in each year.

Data is in a table (.csv file brought into R) with columns YR, MO, DA, TMAX

For the first part, I have tried using subset to pull out all the times the temperature exceeds a value but then I still have to add up each year (time consuming) subset(data, TMAX > 20.86)

I've figured out how to use count, but that gives me all the occurrences in the dataset count(data, vars = "TMAX")

And I have played around with summarise but gotten no where. Any help would be appreciate- especially for the second part of my question- finding the first and last occurrence each year.

Here is sample data. This is SeatlleTMAX (rather than data) as it is the TMAX values for Seattle. YR MO DA TMAX
1909 9 1 28.9
1909 9 2 30.0
1909 9 3 28.3
1909 9 4 33.9
1909 9 5 31.7
1909 9 6 28.3
1909 9 7 26.7
1909 9 8 23.3
1909 9 9 22.2
1909 9 10 17.8
1909 9 11 14.4
1909 9 12 25.6
1909 9 13 23.9
1909 9 14 25.0
1909 9 15 29.4
1909 9 16 28.3
1909 9 17 14.4
1909 9 18 21.7
1909 9 19 14.4
1909 9 20 13.3
1909 9 21 15.6
1909 9 22 20.6
1909 9 23 23.3
1909 9 24 20.0
1909 9 25 21.1
1909 9 26 22.2
1909 9 27 25.6
1909 9 28 22.2
1909 9 29 15.0
1909 9 30 12.2

like image 545
user2113985 Avatar asked Dec 03 '25 15:12

user2113985


1 Answers

Adapting my comment into an answer, taking into account the presented data and OP's comments. Note, code is not checked as dput of data was not obtained.

library("dplyr")

data_summarised <-
    data %>% 
    mutate(date = as.Date(paste(YR, MO, DA, sep = "-"))) %>% # concatenate YR MO DA into an ISO date, convert column into date type 
    filter(TMAX > 20.86) %>%
    group_by(YR) %>%
    summarise(number_of_days = n(), # count number of rows in each group
              first_date = min(date),
              last_date = max(date))
like image 103
Alex Avatar answered Dec 05 '25 04:12

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!