I have a dataset to which I am trying to apply shapiro.test.
test_data <- tibble(
gene = rep(c(LETTERS[1:5]), times = 2, each = 5),
treatment = c(rep('control', times = 25) , rep('treatment', times = 25)),
day = rep(c(1:2), times = 5, each = 5),
data = rnorm(50, mean = 25, sd = 5)
)
# A tibble: 50 x 4
gene treatment day data
<chr> <chr> <int> <dbl>
1 A control 1 28.8
2 A control 1 22.4
3 A control 1 24.8
4 A control 1 20.1
5 A control 1 15.6
6 B control 2 26.5
7 B control 2 26.2
8 B control 2 25.3
9 B control 2 21.4
10 B control 2 35.0
# … with 40 more rows
I created a function to run the test per gene, treatment and day:
normality_test <- function(x, y, z){
with(test_data, shapiro.test(data[gene == x & treatment == y & day == z]))
}
So, if I run normality_test('A', 'control', '1') it will test gene A in control on day 1.
Shapiro-Wilk normality test
data: data[gene == x & treatment == y & day == z]
W = 0.99935, p-value = 0.9998
However, I want the function to loop through all combinations of gene/treatment/day and output each normality test individually, but have not been able to figure it out.
I have been able to create a loop that outputs each row as a separate tibble, but have not been successful in separating each element in the row to add to the normality_test function.
I also experimented with map and lmap, but to no avail.
Thank you.