This is my first posting, I apologize if I don't do this correctly.
I am trying to get the MAX discharge value for that day and previous day (e.g. 2, 4, 8, 16, 32 days) and place them each into a new column. I was able to create these columns using the rollmax with the mutate function. For example here is a subset of my data and what I was able to reproduce:
library(tidyverse) library(dplyr) library(zoo) library(DataCombine) ## subset of data for example daily_flow <- data.frame( date = c('1995-02-25', '1995-02-26', '1995-02-27', '1995-02-28', '1995-03-01', '1995-03-02', '1995-03-03', '1995-03-04', '1995-03-05', '1995-03-06', '1995-03-07', '1995-03-08', '1995-03-09','1995-03-10', '1995-03-11', '1995-03-12', '1995-03-13', '1995-03-14'), discharge = c(NA, NA, NA, NA, 0.58, 0.596, 0.604, 0.617, 0.63, 0.642, 0.657, 0.67, 0.683, 0.696, 0.71, 0.714, 0.74, 0.756) ) ## determining max value for that day and previous days (2, 4, and 8 day) daily_flow_prevdays <- daily_flow %>% mutate("2_d" = rollmax(x = discharge, 2, align = "right", fill = NA)) daily_flow_prevdays <- daily_flow_prevdays %>% mutate("4_d" = rollmax(x = discharge, 4, align = "right", fill = NA)) daily_flow_prevdays <- daily_flow_prevdays %>% mutate("8_d" = rollmax(x = discharge, 8, align = "right", fill = NA)) print(daily_flow_prevdays)
As you can see I was able to produce what I was asking for; however, I still want the initial rows of 2, 4, and 8 day columns to be filled with the max value of the previous cells even if there are no values prior to that date.
**Sorry not sure how to based present this table
This example does appear to be transferring the discharge value to each 2, 4, and 8 day row but since discharge in increasing as the days continue, each day here becomes the new MAX.
I tried using the rollapply function to see if it would not include the previous NAs but I was not successful (also not sure if I was inputting it correctly)
## For example, trying to figure out rollapply on previous 2 days -- but did not work rollapply(daily_flow, width = 2, FUN = function(discharge) mean(discharge, na.rm = TRUE), by = 1, partial = TRUE, fill = NA, align = "right")
Does anyone know how I can replace those NAs with the MAX discharge value even when there is no values (just NAs) prior to that date? My dataset has over 24000 rows, so finding a code where I do all of this in one line, that would be awesome.
I hope this makes sense. Thank you for your help in advance!