Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate mean, standard deviation, n, etc. across columns and create new data frame

Tags:

dataframe

r

I am trying to calculate the number of samples, mean, standard deviation, coefficient of variation, lower and upper 95% confidence limits, and quartiles of this data set across each column and put it into a new data frame.

The numbers below are not necessarily all correct & I didn't fill them all in, just provides an example. These values will be used to create a box plot, hence the need for the quartiles. Rows and columns would be headers in the end. See example below.

Here is the structure:

B1 <- c(8, 6, 13, 6, 27, 104, 18, 3)
B2 <- c(2, 13, 1, 64, 127, 24, 4, 3)
B3 <- c(8, 16, 113, 680, 227, 310, 138, 30)
B4 <- c(238, 46, 613, 69, 7, 14, 4, 8)

x <- data.frame(B1, B2, B3, B4)

> head(x)
    B1  B2  B3  B4
1    8   2   8 238
2    6  13  16  46
3   13   1 113 613
4    6  64 680  69
5   27 127 227   7
6  104  24 310  14

Desired output:

> y
                   B1    B2   B3    B4
n                  8     8     8    8 
mean               23   30    190   125
Stand dev          5    2     34     2
CoeffofVariation   0.3   0.4  0.7   1.3
LowerConfInterval  2    20    35    45
UpperConfInterval  50    120  122   120
LowerQuartile
Median
Upper Quantile
Inter Quartile Range
Minimum
Maximum 
Regression equation
like image 400
kslayerr Avatar asked Jan 03 '23 09:01

kslayerr


2 Answers

As lmo mentioned, you could use sapply, like this:

sapply(x, function(x) c( "Stand dev" = sd(x), 
                         "Mean"= mean(x,na.rm=TRUE),
                         "n" = length(x),
                         "Median" = median(x),
                         "CoeffofVariation" = sd(x)/mean(x,na.rm=TRUE),
                         "Minimum" = min(x),
                         "Maximun" = max(x),
                         "Upper Quantile" = quantile(x,1),
                         "LowerQuartile" = quantile(x,0)
                    )
)

Output:

                            B1         B2         B3         B4
Stand dev            33.604581  44.592600 224.722527 212.086531
Mean                 23.125000  29.750000 190.250000 124.875000
n                     8.000000   8.000000   8.000000   8.000000
Median               10.500000   8.500000 125.500000  30.000000
CoeffofVariation      1.453171   1.498911   1.181196   1.698391
Minimum               3.000000   1.000000   8.000000   4.000000
Maximun             104.000000 127.000000 680.000000 613.000000
Upper Quantile.100% 104.000000 127.000000 680.000000 613.000000
LowerQuartile.0%      3.000000   1.000000   8.000000   4.000000
like image 163
Patricio Moracho Avatar answered May 24 '23 03:05

Patricio Moracho


You could use something like this:

B1 <- c(8, 6, 13, 6, 27, 104, 18, 3)
B2 <- c(2, 13, 1, 64, 127, 24, 4, 3)
B3 <- c(8, 16, 113, 680, 227, 310, 138, 30)
B4 <- c(238, 46, 613, 69, 7, 14, 4, 8)

combDF <- data.frame(cbind(B1,B2,B3,B4))

data_long <- gather(combDF, factor_key=TRUE)

data_long%>% group_by(key)%>%
  summarise(mean= mean(value), sd= sd(value), max = max(value),min = min(value))

and the output would be:

    # A tibble: 4 x 5
     key    mean        sd   max   min
  <fctr>   <dbl>     <dbl> <dbl> <dbl>
1     B1  23.125  33.60458   104     3
2     B2  29.750  44.59260   127     1
3     B3 190.250 224.72253   680     8
4     B4 124.875 212.08653   613     4

You have not specified which confidence level you are looking but the code I posted can be adapted to your problem.

like image 30
DataTx Avatar answered May 24 '23 02:05

DataTx