0
votes

Can I add something to this line to include mins and max as well?

My data consists of 4000 days of data and I would like a simple way of getting min, mean, and max for these days. Ideally in the same output

> head(dd)
    Time  RPH    T  Days
1  00:00:00  6.42 39.6 Day 1
2  00:15:00  6.46 39.7 Day 1
3  00:30:00  6.30 39.6 Day 1
4  00:45:00  6.26 39.4 Day 1
5  01:00:00  6.23 39.3 Day 1
6  01:15:00  6.23 38.5 Day 1

Each day consists of 96 observations. It is very similar format to the Iris data.frame. I have used this for my example.

iris
by(iris[,1:4],iris$Species,colMeans)

output:

>by(iris[,1:4],iris$Species,colMeans)
 iris$Species: setosa
 Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
   5.006        3.428        1.462        0.246 
------------------------------------------------------------ 
iris$Species: versicolor
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
   5.936        2.770        4.260        1.326 
------------------------------------------------------------ 
iris$Species: virginica
Sepal.Length  Sepal.Width Petal.Length  Petal.Width 
   6.588        2.974        5.552        2.026 

This is brilliant but considering the size of the data.frame I'm using:

  • Is there a way I can include min and max to this?
  • Or are there suggestions for something similar?

It would be great to have the values in a table for further manipulation.

2

2 Answers

2
votes

I think the "doBy" package might be useful here. It summarizes the data by groups and returns a data.frame object which will allow you to do any further manipulation. Try this:

install.packages("doBy")
library(doBy)

df <- summaryBy(Sepal.Length + Sepal.Width + Petal.Length + Petal.Width ~ Species,data=iris,
                FUN=function(x){c(min=min(x),max=max(x), mean=mean(x))})

Variables that come before the "~" are the variables you want to summarize while the variables that come after the "~" are the ones you want to group by. So what the above is doing is summarizing: 1. Sepal.Length, 2. Sepal.Width, 3.Petal.Length and 4.Petal.Width by Species.

You can add more summary statistics in the function(x) argument as well.

1
votes

We could use describeBy() from psych package if you want a shortcut.

library(psych)
describeBy(iris[,1:4], iris$Species)
#group: setosa
#             vars  n mean   sd median trimmed  mad min max range skew kurtosis   se
#Sepal.Length    1 50 5.01 0.35    5.0    5.00 0.30 4.3 5.8   1.5 0.11    -0.45 0.05
#Sepal.Width     2 50 3.43 0.38    3.4    3.42 0.37 2.3 4.4   2.1 0.04     0.60 0.05
#Petal.Length    3 50 1.46 0.17    1.5    1.46 0.15 1.0 1.9   0.9 0.10     0.65 0.02
#Petal.Width     4 50 0.25 0.11    0.2    0.24 0.00 0.1 0.6   0.5 1.18     1.26 0.01

A possible base R solution with a slightly different output structure could be using summary() in combination with tapply() - to group by Species - and lapply, to loop over the columns.

lapply(iris, function(x) tapply(x, iris$Species, summary))
#$Sepal.Length
#$Sepal.Length$setosa
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  4.300   4.800   5.000   5.006   5.200   5.800 
#
#$Sepal.Length$versicolor
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  4.900   5.600   5.900   5.936   6.300   7.000 
#
#$Sepal.Length$virginica
#   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#  4.900   6.225   6.500   6.588   6.900   7.900