I have two data frames. I want to transfer the Value
from df B to df A according to the matching values T
and Sample
.
Data frame A
Person T Sample Value
1 A 1
1 A 2
1 A 3
1 B 1
1 B 2
1 B 3
2 A 1
2 A 2
2 A 3
2 B 1
2 B 2
2 B 3
2 C 1
2 C 2
2 C 3
...
Data frame B
T Sample Value
A 1 30.2
A 2 12.5
A 3 77.5
B 1 22.2
B 2 11.0
B 3 23.6
My solution is currently two nested for loops which are way too slow because the data frames are quite large. How can I do this more efficiently, e.g. with the apply family?
for (i in 1:nrow(A)) {
for (p in 1:nrow(B)) {
if(A$T[i] == B$T[p] & A$Sample[i] == B$Sample[p]){
A$Value[i] <- B$Value[p]
}
}
}