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

4
  • Could you provide some sample data? Commented Feb 4, 2022 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 with sample input and desired output that can be used to test and verify possible solutions. Commented Feb 4, 2022 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. Commented Feb 4, 2022 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. Commented Feb 4, 2022 at 20:12

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

2 Comments

this worked! thank you
My pleasure !! I hope you have a good day :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.