问题
I have a data frame with origin and destination names like the following (I will simplify to make it clearer):
Origin Dest Time
A B Mon
B C Wed
C B Fri
I also have a distance matrix that finds the distance between places A
, B
, and C
.
A B C
A 0 8 11
B 8 0 6
C 11 6 0
How would I go about extracting the distance from the distance matrix and inputting it in a separate column (such as df$Distance
) for each row of the data frame?
回答1:
You could reflow the data into long format and merge based on Origin and Dest.
# prepare data
xy <- data.frame(Origin = c("A", "B", "C"),
Dest = c("B", "C", "B"),
Time = c("Mon", "Wed", "Fri"))
mt <- matrix(c(0,8,11,
8,0,6,
11,6,0), byrow = TRUE, ncol = 3)
colnames(mt) <- c("A", "B", "C")
rownames(mt) <- c("A", "B", "C")
mt <- data.frame(mt)
library(tidyr)
# we need "id" column to reflow to long format
mt$Origin <- rownames(mt)
mt <- gather(mt, key = Dest, value = value, -Origin) # reflow data to long format
# merge based on origin and destination
merge(xy, mt, by = c("Origin", "Dest"))
Origin Dest Time value
1 A B Mon 8
2 B C Wed 6
3 C B Fri 6
回答2:
By the power of matrix-indexing (using Roman's data):
mt[as.matrix(xy[c("Origin","Dest")])]
#[1] 8 6 6
This works by matching the Origin
and Dest
to the rownames
and colnames
of the distance matrix.
来源:https://stackoverflow.com/questions/49885368/writing-a-function-that-looks-for-element-in-a-matrix-based-on-values-of-columns