On 6/5/21 10:32 AM, Enrico Gabrielli wrote:
Hello
I just registered on the list.
I am an agricultural technician and I am collaborating on a
research
project on agroforestry and Brown Marmorated Stink Bug (Halyomorpha
halys, abbreviated BMSB).
The project will also address, at least as a draft (it is a very
difficult topic), the modeling of the movements of the BMSB and its
parasitoids.
I am trying to activate an ongoing collaboration with a group of
computer scientists I know.
However for now, to approach the R mailing lists, I have a question
that I would like to post on r-help, but unfortunately there is an
error in the subscription.
I think this mailing list could also be included.
Through kobotoolbox we are collecting data of catches in traps on
farms. Farms register inconsistently.
I am trying to use packages for irregular time series, but I cannot
figure out how to sum the weekly data correctly.
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)
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
To give an example: if a farmer enters the data on Thursday and
Tuesday
of the following week, on Tuesday in the trap he will find
individuals
who were also captured on Friday and Saturday, which formally are
to be
considered in the previous week. I know it's trivial, but maybe
there
is an appropriate statistical method inside some package.
Thank you