I’m looking for advice on how to subtract values from each other listed in two data frames. In the example below with the two data frames A and B, I would like to subtract the values of the 2nd columns from each other on the condition that the first column vectors are matching. For example, when vector X1 is R1 then 5.1 - 5 and 4.8 - 5.
A<-data.frame(cbind(c('R1','R1','R2','R4','R4','R4'),c(5.1,4.8,4.9,5.0,5.0,5.3)))
B<-data.frame(cbind(c('R1','R2','R3','R4'),c(5,4.9,5.2,5.1)))
General R advice: DON'T USE cbind
WHEN IT'S NOT NECESSARY:
A <- data.frame(X1=c('R1','R1','R2','R4','R4','R4'),X2=c(5.1,4.8,4.9,5.0,5.0,5.3))
B <- data.frame(X1=c('R1','R2','R3','R4'),X2=c(5,4.9,5.2,5.1))
(You made factors of those numbers. And you cannot apply arithmetic operators to factors.)
When there are non-unique matches, the merge function returns all possible pairs:
merge(A,B, by=1)
merge(A,B, by=1)[,2] - merge(A,B, by=1)[,3]
#[1] 0.1 -0.2 0.0 -0.1 -0.1 0.2