I am working in R to clean some data in order to analyze it. I have a dataframe (df) that looks like this:
Strain1 Strain2
p1 p2
p2 p3
p3 p4
p4 p5
p5 p1
and a matrix (distmat) that looks like this:
p1 p2 p3 p4 p5
p1 0 0.1 0.3 0.4 0.9
p2 0.1 0 0.5 0.1 0.6
p3 0.3 0.5 0 0.8 0.3
p4 0.4 0.1 0.8 0 0.2
p5 0.9 0.6 0.3 0.2 0
I want to add a column to my data frame which would take Strain1 and Strain2, find the value in the data matrix, pull the value, and put it in a new column and the same row. I need to do this for over 1000 data points.
For reference of the data frame I would get from this example:
Strain1 Strain2 dist
p1 p2 0.1
p2 p3 0.5
p3 p4 0.8
p4 p5 0.2
p5 p1 0.9