kslayerr - 2 years ago 139

R Question

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

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

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.

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**