I have a genotype data frame in R similar to this
ID P1 P2 in1 in2 in3 in4
M01 CC GG CC GG CC GG
M02 TT CC TT TT CC TT
M03 AA GG AA GG GG GG
M04 CC GG CC GG CC GG
M05 GG AA AA GG AA AA
M06 CC GG CC GG CC CC
I want to add a column with the frequencies of all the genotypes in the column P1
. I want to count starting from the column in1
onward per each row. Like the table below:
ID P1 P2 in1 in2 in3 in4 frqP1
M01 CC GG CC GG CC GG 2
M02 TT CC TT TT CC TT 3
M03 AA GG AA GG GG GG 1
M04 CC GG CC GG CC GG 2
M05 GG AA AA GG AA AA 1
M06 CC GG CC GG CC CC 3
I was trying with following code but it doesn't work
df$frqP1 <- rowSums(df[-1] == df$P1)
Any idea?
Does it throw an error (then add the error/warning message), does it give wrong output?