我有一个以长格式排序的数据集,它反映了它们之间的起点,目的地和距离(以km为单位)。考虑到第一个人的数据的子样本如下所示:
from_id to_id distance
1 @34, Exeter College AMT Coffee 0.8535553
2 @34, Exeter College Asia Chinese Takeaway 3.3461476
3 @34, Exeter College BBQ Kebab House 3.3157992
4 @34, Exeter College Best Kebab 1.1460261
5 @34, Exeter College Bodrum Kebab House 0.7994965
我的目的是创建一个权重矩阵,我的问题是,考虑到我只有距离信息,是否有任何可行的方法。
我的数据输入如下:
structure(list(from_id = c("@34, Exeter College", "@34, Exeter College",
"@34, Exeter College", "@34, Exeter College", "@34, Exeter College"
), to_id = c("AMT Coffee", "Asia Chinese Takeaway", "BBQ Kebab House",
"Best Kebab", "Bodrum Kebab House"), distance = c(0.853555311083426,
3.34614761216615, 3.31579922025365, 1.14602607357899, 0.799496508603144
)), .Names = c("from_id", "to_id", "distance"), row.names = c(NA,
5L), class = "data.frame")
答案 0 :(得分:0)
您可以使用
从表中创建矩阵dat_mat <- xtabs(distance ~ from_id + to_id, data = data)
然后创建反距离权重
inv_w <- 1/dat_mat^2