Skip to content
Prev 6309 / 7420 Next

Halyomorpha halys; observation time-series irregular: regularized weekly capture rates

On 6/5/21 10:32 AM, Enrico Gabrielli wrote:
You can do this by using the group_by() function in dplyr. Here's an 
example:


library(dplyr)

# First some fictitious data

# number of capture data points

n = 30

# random integer values for capture counts

captures = as.integer(runif(n)*10)

# 30 random dates from the period between jan 1 and the following 60 days
dates = sort(as.Date("2021-01-01") + sample(1:60, n))

week_nums = strftime(dates, "$U")

captures_df = data.frame("Date" = dates,

 ??? ??? ??? ??? ??? ??? "Captures" = captures,

 ??? ??? ??? ??? ??? ??? "Week_num" = week_nums)

 > head(captures_df)
 ??????? Date Captures Week_num
1 2021-01-06??????? 2?????? 01
2 2021-01-07??????? 2?????? 01
3 2021-01-08??????? 6?????? 01
4 2021-01-09??????? 3?????? 01
5 2021-01-12??????? 9?????? 02
6 2021-01-13??????? 8?????? 02


# Now we can group_by the Week_sum column and sum captures for each week:

captures_weekly = captures_df %>%

 ??? ??? group_by(Week_num) %>%

 ??? ??? summarize(Weekly_sum = sum(Captures))
captures_weekly
# A tibble: 9 x 2
 ? Week_num Weekly_sum
 ? <chr>???????? <int>
1 01?????????????? 13
2 02?????????????? 17
3 03??????????????? 9
4 04?????????????? 25
5 05?????????????? 11
6 06?????????????? 19
7 07??????????????? 8
8 08?????????????? 26
9 09??????????????? 4


The above is for a single year. If you need do weekly sums for 
multi-year data, then specify also the year in the "Weekly_sum" column, 
i.e.:

week_nums = strftime(dates, "%Y-%U")


HTH,

Micha