Walter White
Walter White

Reputation: 51

How to get summary for each column of a list

Lets take a veriable cars as an example. Cars has two columns cars$speed, cars$dist.

I want to write a function that will print in one step summary for each column of a veriable(in this case cars). It would look like:

f<-function(x){
#do some stuff
}

The result:

name of first column:
 Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
4.0    12.0    15.0    15.4    19.0    25.0 
name of second column:
     Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
       2.00   26.00   36.00   42.98   56.00  120.00 

How do I do that?

Upvotes: 1

Views: 39609

Answers (2)

G. Cocca
G. Cocca

Reputation: 2541

If you want as output a list

f <- lapply(cars, summary)

if you want a matrix

f <- sapply(cars, summary)

Upvotes: 5

Raphael K
Raphael K

Reputation: 2353

If all you want is a summary of quantiles and mean, median, then just call summary() on your data frame. It will give you a summary for each column. If you want to call other functions...

There's a great package for that, dplyr. Take a look at summarise_each() and summarise().

Say you want to find the mean of each column and have the output be its own data frame:

install.packages('dplyr')
library(dplyr)
new_df <- summarise_each(cars, funs(mean))

## Subsetting to only summarize specific columns
new_df <- summarise_each(cars[, c('speed', 'dist')], funs(mean))

You can also compute summaries based on different groups in your data, using the group_by() function. You didn't ask about that so I'll just stop here.

Upvotes: 2

Related Questions