I have the following data frame with 345 rows and 237 columns in R:
snp1 snp2 snp3 ... snp237
0 1 2 ... 0
0 1 1 ... 1
1 1 2 ... 2
1 0 0 ... 0
... ... ... ...
2 2 1 ... 0
I want to apply the following function in each column:
D=(number of 0)/(number of rows)
H=(number of 1)/(number of rows)
R=(number of 2)/(number of rows)
p=D+(0.5*H)
q=R+(0.5*H)
Lastly, I want to store the "p" and "q" for each snp in a vector. This function have calculate "p" and "q" for each snp in a single command of R. It is possible?
The output is:
snp1 snp2 snp3 ... snp237
p1 p2 p3 ... ... p237
q1 q2 q3 ... ... q237
Thanks in advance.
f1 <- function(x) {H <- mean(x == 1);list(p = mean(x == 0) + (0.5 * H), q = mean(x == 2) + (0.5 * H))}; library(dplyr); df1 %>% summarise_all(f1) %>% unnest