erik-stengel
erik-stengel

Reputation: 439

calculate statistical sizes grouped by two columns

In R I have data like that

> a
 ID    row   variant  size  weight    
<dbl>  <chr> <chr>    <dbl> <dbl> 
 1     7     1        22.5  0.00609 
 2     7     1        23.5  0.00704
 3     7     2        22.5  0.00598
 4     8     1        22.5  0.00536
 5     8     2        35.5  0.04043

I would like to calculate statitical sizes (mean, median, sd) for each row and variant. E.g. for the column size, the mean should be calculated for each row-variant (7-1, 7-2, 8-1, 8-2, ...). So far I am doing this by adding another column RV

a$RV<-paste(a$row, a$variant, sep='.')

and using tapply()

> tapply(a$size,a$RV,median)
        5.1         5.2         5.3         6.1         6.2         6.3         7.1         7.2 
0.010555175 0.010509383 0.010497835 0.010467480 0.010540752 0.010523763 0.009427733 0.010499771 
        7.3 
0.010504420 

Is there another, better opportunity?

Upvotes: 0

Views: 32

Answers (1)

jdsimkin04
jdsimkin04

Reputation: 161

You can use group_by() and summarize(). Here's an example

library(tidyverse)
your_data %>%
group_by(row, variant) %>%
summarize(
mean = mean(size),
median = median(size),
sd = sd(size)
)

Upvotes: 1

Related Questions