Yes, its quite easy.
Let me know if this helps you:
my_function_name <- function(df){
table1 <- df %>%
group_by(org) %>%
tally()
table2 <- df %>%
group_by(org) %>%
mutate_at(vars(hours), ~replace_na(., 0)) %>%
summarise(hours = sum(hours))
table3 <- table1 %>%
inner_join(table2)
table3$average <- table3$hours/table3$n
return(list(table1,table2,table3))
}
# Calling the function
results <- my_function_name(df)
results$table1
results$table2
results$table3
In this case I used the function to retrieve all the tables. If you only want the final number table3$hours/table3$n
what we can do is change the return of the function:
my_function_name <- function(df){
table1 <- df %>%
group_by(org) %>%
tally()
table2 <- df %>%
group_by(org) %>%
mutate_at(vars(hours), ~replace_na(., 0)) %>%
summarise(hours = sum(hours))
table3 <- table1 %>%
inner_join(table2)
table3$average <- table3$hours/table3$n
return(table3$average)
}
# Calling the function
results <- my_function_name(df)
results