1

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?

2 Answers 2

5

If you want as output a list

f <- lapply(cars, summary)

if you want a matrix

f <- sapply(cars, summary)
2
  • Iit looks really simple. Do you know how to create a chart for each column and save it. I mean each chart in a new file or what would be better multiple plots in the same file? Well, I cant figure it out. If you dont mind turoring it would be helpful. Commented May 3, 2016 at 13:59
  • 2
    @WalterWhite that's a separate issue, consider asking a new question if you dont find a suitable answer pdf('plots.pdf'); lapply(cars, plot); dev.off()
    – rawr
    Commented May 3, 2016 at 14:52
2

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.

8
  • Looks nice. what about plots? I would like to make a hist chart or density char for each column? How do I do that? Commented May 3, 2016 at 13:38
  • 1
    Ha, I don't mind tutoring, but surely you can google these things a bit. You can make a histogram in base R using the hist() function and specifying which column you want to count. So, maybe hist(cars$speed) or hist(cars$dist). I forget the density plot call off the top of my head, but I suggest familiarizing yourself with ggplot2, it's fantastic. If you think my original answer is the correct one, you can use the check mark to mark it as the answer.
    – Raphael K
    Commented May 3, 2016 at 13:40
  • I try to do the same with lapply/apply function. Well I would like to gets histogram of each column and save those hisogram(multiple hisograms in the same file). Do you know how to do this that way? I can't find the anwer in google. Commented May 3, 2016 at 14:02
  • Do it in two steps. Store your plots in a list using lapply/apply, then use lapply with the save() function.
    – Raphael K
    Commented May 3, 2016 at 14:06
  • I found function that does exacly what I want. How to simplyfy this? multi.hist <- function(x) {nvar <- dim(x)[2] #number of variables nsize=trunc(sqrt(nvar))+1 #size of graphic old.par <- par(no.readonly = TRUE) # all par settings which can be changed par(mfrow=c(nsize,nsize)) #set new graphic parameters for (i in 1:nvar) { name=names(x)[i] #get the names for the variables hist(x[,i],main=name,xlab=name) } #draw the histograms for each variable on.exit(par(old.par)) #set the graphic parameters back to the original } Commented May 3, 2016 at 14:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.