Summary Function in R - error each time

I have been trying to create a function to summarise a bunch of different variables that are present as columns in a data frame. I am aiming to use a function to avoid lots of repetition.

This is the code I am attempting to use. Essentially, I want to be able to enter the data frame info (x) and column info (y) to then return the output (z) as a data frame that shows me the min, max, mean ,sd for each of the elements of a column within a data frame:

function(x, y)
z <- x %>%
    group_by(y) %>%
    summarise(min = min(Total_Cost), max = max(Total_Cost), mean = mean(Total_Cost,na.rm = TRUE), sd = sd(Total_Cost))

Unfortunately, I get this error:

Error in UseMethod("group_by") : 
no applicable method for 'group_by' applied to an object of class "NULL"

I may be doing something completely wrong and I may be trying to do something that’s easy in a poor way. I’d be grateful for any advice on getting this kind of function to work.

1 Like

Hello @samjee410 ,

How are you calling the function? The argument for y is NULL so that’s quite odd.