r - Wrap function only showing result of last function

66 Views Asked by At

I'm attempting to write my first wrap function, showing the mean, variance, stdev, and summary for a vector in r

des_function = function(y)
  {
  mean(y); 
  var(y);
  sd(y);
  summary(y);
  }
des_function(even)

but it's only showing the results of the summary function:

des_function(even)
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
    2.0   251.5   501.0   501.0   750.5  1000.0

Thanks!

2

There are 2 best solutions below

3
On BEST ANSWER

I would suggest this slight change:

#Function
des_function = function(y)
{
  list(
  mean(y), 
  var(y),
  sd(y),
  summary(y)
  )
}
#Apply
des_function(even)
0
On

We could create a tibble and return the tibble

library(tibble)
des_function <- function(y) {
        tibble(Mean = mean(y), Var = var(y), SD = sd(y), Summary = list(summary(y)))
 }