What have you tried so far? what is your specific problem?
Could you please turn this into a self-contained REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.
If you've never heard of a reprex before, you might want to start by reading this FAQ:
I have daily data in csv format and the data frame is below. I wanted to find the sum and maximum of four and six days. However, I tried the commands below. When I use tapply i get the error and when I applied the rollapply command the data is rolled for four and six days like stepping one day ahead.
Can you help on this?
library(hydroTSM)
data.frame(
Year = c(1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L),
Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L),
Day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L),
Rain = c(0, 0, 1, 1, 1, 1, 0, 2, 2, 0, 4, 0, 0, 5, 0, 0, 3, 0, 10, 4,
10, 7, 10, 11, 0, 1, 5, 0, 0, 2, 0, 0, 0, 5, 0, 0, 1, 6, 0, 3,
0, 1, 0, 0, 1, 0, 0, 0, 0, 0)
)
day4=tapply(b$Rain,b$Year,b$Month,b$Day, width=4,sum,na.rm=TRUE)
Error in match.fun(FUN) :
'b$Month' is not a function, character or symbol
day4=rollapply(Rain,width=4, FUN=sum)
day4max=tapply(b$Rain,b$Year,b$Month,b$Day,width=4,max,na.rm=TRUE)
day4=rollapply(Rain,width=4, FUN=max)
Its not clear to me what are you looking for, is a roll sum or you want to aggregate by 4 days periods? see examples below
df <- data.frame(
Year = c(1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L, 1904L,
1904L, 1904L, 1904L, 1904L, 1904L),
Month = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L),
Day = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L,
27L, 28L, 29L, 30L, 31L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L),
Rain = c(0, 0, 1, 1, 1, 1, 0, 2, 2, 0, 4, 0, 0, 5, 0, 0, 3, 0, 10, 4,
10, 7, 10, 11, 0, 1, 5, 0, 0, 2, 0, 0, 0, 5, 0, 0, 1, 6, 0, 3,
0, 1, 0, 0, 1, 0, 0, 0, 0, 0)
)
library(zoo)
library(dplyr)
library(tibbletime)
# Rollsum
df %>%
mutate(roll_sum = rollsum(Rain, 4, fill = NA, align = "right"))
#> Year Month Day Rain roll_sum
#> 1 1904 1 1 0 NA
#> 2 1904 1 2 0 NA
#> 3 1904 1 3 1 NA
#> 4 1904 1 4 1 2
#> 5 1904 1 5 1 3
#> 6 1904 1 6 1 4
#> 7 1904 1 7 0 3
#> 8 1904 1 8 2 4
#> 9 1904 1 9 2 5
#> 10 1904 1 10 0 4
#> 11 1904 1 11 4 8
#> 12 1904 1 12 0 6
#> 13 1904 1 13 0 4
#> 14 1904 1 14 5 9
#> 15 1904 1 15 0 5
#> 16 1904 1 16 0 5
#> 17 1904 1 17 3 8
#> 18 1904 1 18 0 3
#> 19 1904 1 19 10 13
#> 20 1904 1 20 4 17
#> 21 1904 1 21 10 24
#> 22 1904 1 22 7 31
#> 23 1904 1 23 10 31
#> 24 1904 1 24 11 38
#> 25 1904 1 25 0 28
#> 26 1904 1 26 1 22
#> 27 1904 1 27 5 17
#> 28 1904 1 28 0 6
#> 29 1904 1 29 0 6
#> 30 1904 1 30 2 7
#> 31 1904 1 31 0 2
#> 32 1904 2 1 0 2
#> 33 1904 2 2 0 2
#> 34 1904 2 3 5 5
#> 35 1904 2 4 0 5
#> 36 1904 2 5 0 5
#> 37 1904 2 6 1 6
#> 38 1904 2 7 6 7
#> 39 1904 2 8 0 7
#> 40 1904 2 9 3 10
#> 41 1904 2 10 0 9
#> 42 1904 2 11 1 4
#> 43 1904 2 12 0 4
#> 44 1904 2 13 0 1
#> 45 1904 2 14 1 2
#> 46 1904 2 15 0 1
#> 47 1904 2 16 0 1
#> 48 1904 2 17 0 1
#> 49 1904 2 18 0 0
#> 50 1904 2 19 0 0
# Aggregation by 4 days
df %>%
mutate(date = as.Date(paste(Year, Month, Day, sep = "-"))) %>%
select(date, Rain) %>%
as_tbl_time(index = date) %>%
collapse_by("4 days", side = "start", clean = TRUE) %>%
group_by(date) %>%
summarise(days4 = sum(Rain))
#> # A time tibble: 13 x 2
#> # Index: date
#> date days4
#> <date> <dbl>
#> 1 1904-01-01 2
#> 2 1904-01-05 4
#> 3 1904-01-09 6
#> 4 1904-01-13 5
#> 5 1904-01-17 17
#> 6 1904-01-21 38
#> 7 1904-01-25 6
#> 8 1904-01-29 2
#> 9 1904-02-02 5
#> 10 1904-02-06 10
#> 11 1904-02-10 1
#> 12 1904-02-14 1
#> 13 1904-02-18 0
Created on 2019-03-19 by the reprex package (v0.2.1)
2 posts were split to a new topic: Generalized Extreme Value Distribution (GEV) - Only get plot background
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.