Count the number of elements between 2 dates conditionally on a variable in R

M. Beausoleil

I'm trying to count the number of precipitation below a certain threshold (let's say less or equal than 50) between two dates.

Basically, I have a vector cuts that contains the dates that I want to count between inclusively. I want to use the cuts vector to "subset" the dataset in different bins and than count the number of events where it was raining less than 50 mm of rain.

I'm using dplyr and a for loop at the moment, but nothing is working.

set.seed(12345)
df = data.frame(date = seq(as.Date("2000/03/01"), as.Date("2002/03/01"), "days"), 
                precipitation = rnorm(length(seq(as.Date("2000/03/01"), as.Date("2002/03/01"), "days")),80,20))
cuts = c("2001-11-25","2002-01-01","2002-02-18","2002-03-01")
for (i in 1:length(cuts)) {
  df %>% summarise(count.prec = if (date > cuts[i] | date < cuts[i+1]) {count(precipitation <= 50)})
}

But I have this error message:

Error: no applicable method for 'group_by_' applied to an object of class "logical"
In addition: Warning message:
In if (c(11017, 11018, 11019, 11020, 11021, 11022, 11023, 11024,  :
  the condition has length > 1 and only the first element will be used

This is not working either:

for (i in 1:length(cuts)) {
  df %>% if (date > cuts[i] | date < cuts[i+1])%>% summarise(count.prec = count(precipitation <= 50))
}
Steven Beaupré

You could try:

df %>%
  group_by(gr = cut(date, breaks = as.Date(cuts))) %>%
  summarise(res = sum(precipitation <= 50))

Which gives:

# A tibble: 4 × 2
          gr   res
      <fctr> <int>
1 2001-11-25     1
2 2002-01-01     4
3 2002-02-18     2
4         NA    40

Or as per mentioned by @Frank - you could replace summarise() by tally(precipitation <= 50)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Count number of days between dates, ignoring weekends using pyspark

From Dev

Total number of months (Count) between 2 dates

From Dev

Count number of Saturdays between two dates - MySql

From Dev

Count unique number of days between 2 dates where each day will have multiple rows in MySQL

From Dev

Find out number of months between 2 dates

From Dev

Number of months between 2 dates

From Dev

Count the number of users between certain dates

From Dev

Count number of days between 2 dates in JPA

From Dev

Calculate number of days between two dates in r

From Dev

Days between dates for any day-count basis in R

From Dev

Count Number of Rows Between Two Dates BY ID in a Pandas GroupBy Dataframe

From Dev

SQL Count between dates

From Dev

How to count same number of elements between two arrays, excluding duplicates

From Dev

Count number of times a variable is repeated continuously in R

From Dev

Count number of specific elements in between other elements in list

From Dev

How to count the number of sundays between two dates

From Dev

How to count fractional days between 2 dates

From Dev

Number of days between dates?

From Dev

Find out number of months between 2 dates

From Dev

count number of words between 2 fixed words

From Dev

Count Number of Rows Between Two Dates BY ID in a Pandas GroupBy Dataframe

From Dev

calculate number of days between 2 dates in php

From Dev

How to count the number of sundays between two dates

From Dev

Count number of including weeks between 2 dates

From Dev

How to count and sum a field between 2 dates?

From Dev

How to create a vector between 2 dates in R

From Dev

pivot in group by ranges and count number elements in R

From Dev

Scala: Difference between 2 dates in number of months

From Dev

kdb q - count subtable between 2 dates

Related Related

  1. 1

    Count number of days between dates, ignoring weekends using pyspark

  2. 2

    Total number of months (Count) between 2 dates

  3. 3

    Count number of Saturdays between two dates - MySql

  4. 4

    Count unique number of days between 2 dates where each day will have multiple rows in MySQL

  5. 5

    Find out number of months between 2 dates

  6. 6

    Number of months between 2 dates

  7. 7

    Count the number of users between certain dates

  8. 8

    Count number of days between 2 dates in JPA

  9. 9

    Calculate number of days between two dates in r

  10. 10

    Days between dates for any day-count basis in R

  11. 11

    Count Number of Rows Between Two Dates BY ID in a Pandas GroupBy Dataframe

  12. 12

    SQL Count between dates

  13. 13

    How to count same number of elements between two arrays, excluding duplicates

  14. 14

    Count number of times a variable is repeated continuously in R

  15. 15

    Count number of specific elements in between other elements in list

  16. 16

    How to count the number of sundays between two dates

  17. 17

    How to count fractional days between 2 dates

  18. 18

    Number of days between dates?

  19. 19

    Find out number of months between 2 dates

  20. 20

    count number of words between 2 fixed words

  21. 21

    Count Number of Rows Between Two Dates BY ID in a Pandas GroupBy Dataframe

  22. 22

    calculate number of days between 2 dates in php

  23. 23

    How to count the number of sundays between two dates

  24. 24

    Count number of including weeks between 2 dates

  25. 25

    How to count and sum a field between 2 dates?

  26. 26

    How to create a vector between 2 dates in R

  27. 27

    pivot in group by ranges and count number elements in R

  28. 28

    Scala: Difference between 2 dates in number of months

  29. 29

    kdb q - count subtable between 2 dates

HotTag

Archive