Home > Back-end >  How to use the summary{base} function without displaying the Quantiles and Median?
How to use the summary{base} function without displaying the Quantiles and Median?

Time:01-27

I am working in Rstudio, on an Rmarkdown document, and want to knit a clean file. I have data about women and want to summarize them in the file, out of a chunk. But the summary() function gives automatically the 1st and 3rd quantiles like (here for their age):

>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
  17.00   25.00   35.00   31.92   35.00   50.00 

I would like a cleaner, simpler display, by only showing the min, max and mean (deleting quantiles and the median) like:

>    Min.   Mean    Max. 
    17.00   31.92   50.00 

Is there an option of the function to solve that? Or another function that does so?

I went through R documentation on the function but did not manage to find if there was an option for that, even though there is this

quantile.type integer code used in quantile(*, type=quantile.type) for the default method. I did not understand.

Thanks

CodePudding user response:

if you save the output in its own object, you can then call the specific columns you want:

mysumm <- summary(iris$Sepal.Length)

mysumm[c(1,4,6)]

    Min.     Mean     Max. 
4.300000 5.843333 7.900000 

CodePudding user response:

As inspired by Phil answer, You can also create your own function

my_function <- function(x) { 
  
  mmin <- round(min(x),2)
  mmean <- round(mean(x),2)
  mmax <- round(max(x),2)
  
  vec_fun <- c(mmin,mmean,mmax)
  names(vec_fun) <- c("Min.", "Mean", "Max.")
  
  vec_fun
  
}

> my_function(iris$Sepal.Length)
Min. Mean Max. 
4.30 5.84 7.90 
  • Related