I have written interactive code that works, but I am struggling to write a function that can take the place of my copy-and-paste efforts.
The ingredients:
a data frame with annual numerator data.
Other columns contain demographic data.
I need to compute three year running totals for the numerator
My code is found in the function func_3()
I do not know what goes inside the parentheses in the following cases.
I do not know how to use parameters in this setting. Otherwise, I am OK.
group_by(what do I put in here?)
arrange(what do I put in here?)
mutate(what do I put in here?)
select(what do I put in here?)
library(purrr)
library(dplyr)
library(tidyr)
library(slider) # for grouping consecutive years
# The sample data
set.seed(2021)
EVENT_YEAR = 2010:2015
RE = c('white', 'black', 'Asian')
City = c('Oakland', 'San Francisco', 'San Jose')
Note = 1:3
demoDF_N = expand.grid(EVENT_YEAR = EVENT_YEAR, RE = RE, City = City, Note = Note)
demoDF_N$Numerator = sample(3:10, 162, replace = TRUE)
Sample calls
# Function calls
res1 = func_3(demoDF_N, demoDF_D, EVENT_YEAR)
res2 = func_3(demoDF_N, demoDF_D, EVENT_YEAR, RE)
res3 = func_3(demoDF_N, demoDF_D, EVENT_YEAR, City)
res4 = func_3(demoDF_N, demoDF_D, EVENT_YEAR, City, RE)
This is the function that needs fixing
func_3 = function(df1, ... ){
result_3_N = df1 %>% # 3-year running average
group_by(Note, ...) %>% # Need help inside all ( )
arrange(Note,..., EVENT_YEAR) %>%
mutate(Numerator_UPDATED = slider::slide_dbl(Numerator, sum, .before = 1, .after = 1, .complete = TRUE)) %>%
select(Note,..., EVENT_YEAR, Numerator_UPDATED) %>% ungroup()
result_3_N = result_3_N %>% rename(Numerator = Numerator_UPDATED) # simple rename
result_3_N = result_3_N %>% filter(!is.na(Numerator)) # filter out rows not based on 3 full years
# Get EVENT_YEAR to display range of years, e.g., 2008-2010
result_3_N$EVENT_YEAR =
paste(as.integer(as.character(result_3_N$EVENT_YEAR)) - 1, '-',
as.integer(as.character(result_3_N$EVENT_YEAR)) + 1, sep = '')
result_3_N
}