I have a dataframe containing observations from the control and the experimental group with replicates for each subject: Here is an example of my dataframe:
subject group replicate value
A control 1 10
A control 2 15
A experim 1 40
A experim 2 45
B control 1 5
B experim 1 30
C control 1 50
C experim 1 NA
I'd like to pair each control observation with its corresponding experimental one in order to calculate the ratio between the paired values. The desired output:
subject replicate control experim ratio
A 1 10 40 4
A 2 15 45 3
B 1 5 30 6
C 1 50 NA NA
Please, note that the number of replicates for subjects can vary (A has two replicates, B only one, C has one with a missing value). Ideally, I'd like to see this implemented with dplyr and pipes.