0

Not too good with functions. Is there a way to write the below script as a function? I have a list of dataframes that I want to apply the below scripts to.

head(iris)

iris1 <- iris %>% 
  group_by(Species) %>% 
      mutate_at(vars(Petal.Length), ~replace_na(., 0)) %>%
  summarise(Petal.Length = sum(Petal.Length))


iris2 <- iris %>% 
  group_by(Species) %>% 
  tally()

iris3 <- iris2 %>%
  inner_join(iris1)

iris3$average <- iris3$Petal.Length/iris3$n

enter image description here

J.Doe
  • 165
  • 7
  • Could you provide some sample data? – Peter Feb 04 '22 at 19:54
  • 2
    What do you mean by a single function? What would be the inputs to that function? Or do you mean a single line of code? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 04 '22 at 19:55
  • Yeah, I'm not sure what you're asking. You could just literally just put this existing code in a function with no difficulty (you'd just have to `return(table3)` at the end. – divibisan Feb 04 '22 at 20:01
  • Added sample data. I have a list of dataframes and would like to apply these scripts to all of them. Not sure how to create a function with them. – J.Doe Feb 04 '22 at 20:12

1 Answers1

1

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
AugtPelle
  • 549
  • 1
  • 10