Writing a function that looks for element in a matrix based on values of columns of data frame

夙愿已清 提交于 2020-01-24 13:20:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!