通过距离矩阵向上计算沿矢量的总距离

时间:2017-08-22 19:18:32

标签: r euclidean-distance

我有一个row.names排序的data.frame,我想计算每一行的总距离(例如Var1 --> Var2 --> Var3 --> Var4 --> Var5 --> Var6 --> Var7)。

l1 <- as.list(c(row.names(subset(example, Timepoint=='T1'))))
l2 <- as.list(c(row.names(subset(example, Timepoint=='T2'))))
l3 <- as.list(c(row.names(subset(example, Timepoint=='T4'))))
l4 <- as.list(c(row.names(subset(example, Timepoint=='T5'))))
l5 <- as.list(c(row.names(subset(example, Timepoint=='T6'))))
l6 <- as.list(c(row.names(subset(example, Timepoint=='T7'))))
l7 <- as.list(c(row.names(subset(example, Timepoint=='T8'))))
all.paths <- expand.grid(l1, l2, l3, l4, l5, l6, l7)

all.paths的负责人返回:

       Var1      Var2    Var3    Var4    Var5    Var6    Var7
1   FOV2.T1   FOV2.T2 FOV2.T4 FOV2.T5 FOV2.T6 FOV2.T7 FOV2.T8
2 FOV2.T1.1   FOV2.T2 FOV2.T4 FOV2.T5 FOV2.T6 FOV2.T7 FOV2.T8
3 FOV2.T1.2   FOV2.T2 FOV2.T4 FOV2.T5 FOV2.T6 FOV2.T7 FOV2.T8
4   FOV2.T1 FOV2.T2.1 FOV2.T4 FOV2.T5 FOV2.T6 FOV2.T7 FOV2.T8
5 FOV2.T1.1 FOV2.T2.1 FOV2.T4 FOV2.T5 FOV2.T6 FOV2.T7 FOV2.T8
6 FOV2.T1.2 FOV2.T2.1 FOV2.T4 FOV2.T5 FOV2.T6 FOV2.T7 FOV2.T8

此处的目标是将另一列附加到all.paths,并列出每行的距离总和。我可以像这样计算距离矩阵:

distance.matrix <- as.matrix(dist(example[,c('Centre.int.X','Centre.int.Y','Centre.int.Z')], upper = TRUE, diag = TRUE))

example定义为:

structure(list(Timepoint = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 
3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 7L, 7L, 7L), .Label = c("T1", 
"T2", "T4", "T5", "T6", "T7", "T8"), class = "factor"), Volume = c(0.291, 
0.199, 0.208, 0.257, 0.177, 0.18, 0.186, 0.257, 0.147, 0.053, 
0.055, 0.042, 0.131, 0.085, 0.046, 0.177, 0.138, 0.228, 0.081
), Surface = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1), Intensity = c(33672.323, 30814.852, 30391.92, 29759.664, 
26733.458, 26152.827, 11392.416, 14630.421, 9785.8, 20470.483, 
20700.367, 18450.261, 22655.197, 21340.957, 24945, 7021.5, 9316.467, 
5062.823, 3367.795), Centre.X = c(71.219, 71.201, 71.278, 71.201, 
74.08, 10.713, 73.927, 71.278, 10.801, 71.268, 10.753, 74.033, 
71.283, 73.921, 10.785, 10.774, 72.989, 71.235, 10.84), Centre.Y = c(11.071, 
11.069, 11.078, 11.069, 17.163, 13.859, 17.16, 11.078, 13.91, 
11.191, 13.908, 17.262, 11.071, 17.16, 13.861, 13.928, 15.315, 
11.1, 15.293), Centre.Z = c(2.256, 2.226, 1.82, 2.226, 2.233, 
2.224, 1.83, 1.82, 1.835, 2.8, 2.56, 2.8, 1.814, 1.843, 2, 1.762, 
2.4, 1.871, 2), Centre.int.X = c(71.221, 10.748, 73.989, 71.196, 
74.071, 10.708, 73.929, 71.285, 10.801, 71.271, 10.75, 74.035, 
71.285, 73.924, 10.789, 10.771, 72.983, 71.231, 10.839), Centre.int.Y = c(11.068, 
13.867, 17.169, 11.071, 17.168, 13.86, 17.159, 11.084, 13.906, 
11.192, 13.903, 17.264, 11.071, 17.157, 13.867, 13.892, 15.32, 
11.094, 15.286), Centre.int.Z = c(2.246, 2.256, 2.25, 2.261, 
2.276, 2.249, 1.829, 1.818, 1.842, 2.8, 2.54, 2.8, 1.834, 1.858, 
2, 1.657, 2.4, 1.955, 2), FOV = c("FOV2", "FOV2", "FOV2", "FOV2", 
"FOV2", "FOV2", "FOV2", "FOV2", "FOV2", "FOV2", "FOV2", "FOV2", 
"FOV2", "FOV2", "FOV2", "FOV2", "FOV2", "FOV2", "FOV2"), ID = c("FOV2-T1", 
"FOV2-T1", "FOV2-T1", "FOV2-T2", "FOV2-T2", "FOV2-T2", "FOV2-T4", 
"FOV2-T4", "FOV2-T4", "FOV2-T5", "FOV2-T5", "FOV2-T5", "FOV2-T6", 
"FOV2-T6", "FOV2-T6", "FOV2-T7", "FOV2-T8", "FOV2-T8", "FOV2-T8"
), ColorTimepoint = structure(c(6L, 6L, 6L, 5L, 5L, 5L, 2L, 2L, 
2L, 3L, 3L, 3L, 1L, 1L, 1L, 4L, 7L, 7L, 7L), .Label = c("#00A9FF", 
"#00BE67", "#00BFC4", "#C77CFF", "#CD9600", "#F8766D", "#FF61CC"
), class = "factor")), .Names = c("Timepoint", "Volume", "Surface", 
"Intensity", "Centre.X", "Centre.Y", "Centre.Z", "Centre.int.X", 
"Centre.int.Y", "Centre.int.Z", "FOV", "ID", "ColorTimepoint"
), row.names = c("FOV2.T1", "FOV2.T1.1", "FOV2.T1.2", "FOV2.T2", 
"FOV2.T2.1", "FOV2.T2.2", "FOV2.T4", "FOV2.T4.1", "FOV2.T4.2", 
"FOV2.T5", "FOV2.T5.1", "FOV2.T5.2", "FOV2.T6", "FOV2.T6.1", 
"FOV2.T6.2", "FOV2.T7", "FOV2.T8", "FOV2.T8.1", "FOV2.T8.2"), class = "data.frame")

0 个答案:

没有答案