I have the following data frame example:
dates <- seq(as.Date('2018-12-29'), as.Date('2020-01-01'), by = 'days') Variable1 <- sample.int(20, 369, replace = TRUE) Variable2 <- sample (20,369, replace = TRUE) df <- data.frame(dates, Variable1, Variable2)
My RShiny app has a selectInput (ID="selectvariable") with choices = "Variable1", "Variable2" and a selectInput (ID= "selectduration") with choices = "daily", "weekly", "monthly"
My goal is to create a ggplot with dates on the x axis and the input of "selectvariable" on the y axis. If the input of "selectduration" == "daily" it should plot the selected variable as it appears in the data frame.
If the input of "selectduration" == "weekly" it should plot THE WEEKLY AVERAGE for each week across the data frame for the selected variable.
If the input of "selectduration" == "monthly" it should plot THE MONTHLY AVERAGE for each month across the data frame for the selected variable.
It is a fairly simple app, however I am stuck on how to create these "weekly" and "monthly" averages and plot them. I do not think that creating additional columns of weekly/monthly averages is an appropriate choice because in reality there are many other variables. I want this to be as automated as possible.
I would love to hear what everyone has to say! Thank you!