I have written a function to calculate the mean values from monthly data within a year (starting in the previous Dec to the current Sep). Each dataframe (and there are many) that I'm feeding through this function is presently named for the variable that I'm interested in. But I want the output to use the name of the dataframe as the variable.
Example:
year <- c(2000:2020)
`1` <- runif(21, 0,10)
`2` <- runif(21, 0,10)
`3` <- runif(21, 0,10)
`4` <- runif(21, 0,10)
`5` <- runif(21, 0,10)
`6` <- runif(21, 0,10)
`7` <- runif(21, 0,10)
`8` <- runif(21, 0,10)
`9` <- runif(21, 0,10)
`10` <- runif(21, 0,10)
`11` <- runif(21, 0,10)
`12` <- runif(21, 0,10)
dogs <- as.data.frame(cbind(year, `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9`, `10`, `11`, `12`))
fun_annual <- function(df) {
name = deparse(substitute(df)) #I know I can use this to get the name of the dataframe
df %>%
mutate(lag12 = lag(`12`)) %>%
rowwise() %>% # complete next calculations row by row
mutate(name = mean(c_across(c(`1`:`9`, lag12)))) %>% #mean from prev Dec to Sep
select(year, name) #by this point 'name' should be 'dogs' (name of original dataframe
}
# Final function should return a dataframe with 2 variables: year and dogs
Bonus points if someone can also help be figure out how to put the months in the function, so I don't have to change the months within the function each time! Especially if I use more months from the previous year (beyond Dec)