0

In R I'm trying to profile the columns of a data frame. This is the data frame:

> library(MASS)
> data<-iris[1:5,1:4]
> data
  Sepal.Length Sepal.Width Petal.Length Petal.Width
1          5.1         3.5          1.4         0.2
2          4.9         3.0          1.4         0.2
3          4.7         3.2          1.3         0.2
4          4.6         3.1          1.5         0.2
5          5.0         3.6          1.4         0.2

I want the result of the profiling to look something like this:

              min  max mean
Sepal.Length  4.6  5.1    5
 Sepal.Width  3.0  3.6    5
Petal.Length  1.3  1.5    3
 Petal.Width  0.2  0.2    1

There could be many more functions I want to apply to the columns.

I'm able to get the data I want with this command:

library(dplyr)
data %>% summarise_all(funs(min, max, mean))

However, neither the shape nor the row/column names are as desired. Is there an elegant way of achieving what I want?

matthiash
  • 3,105
  • 3
  • 23
  • 34
  • 1
    You could reshape first with `tidy::gather`, then `group_by` variable and do a regular `summarise`. [Also see this.](http://stackoverflow.com/questions/21644848/summarizing-multiple-columns-with-dplyr?rq=1) – Axeman Feb 08 '17 at 14:48

3 Answers3

2

Oneliner with base R:

t(sapply(data, summary))[, c('Min.', 'Max.', 'Mean')]
GGamba
  • 13,140
  • 3
  • 38
  • 47
1
library(plyr)    
t(sapply(data, each(min,max,mean)))
matthiash
  • 3,105
  • 3
  • 23
  • 34
  • @DavidArenburg: its from the plyr package. Combines multiple functions into a single function. I added the library function to the answer for clarity. – matthiash Feb 08 '17 at 15:14
0

Using dplyr to allow use of any functions

library(dplyr)
library(tidyr)

data %>%
  gather() %>%
  group_by(key) %>%
  summarise_all(funs(min, max, mean))
manotheshark
  • 4,297
  • 17
  • 30