我想計算具有30.000個觀測值的數據幀的行之間的歐幾里德距離。一個簡單的方法是dist功能(例如dist(數據))。但是,由於我的數據幀很大,這需要花費太多時間。以更快的方式計算歐幾里德距離
某些行包含缺少的值。我不需要兩行之間包含缺失值的行之間的距離,也不需要行之間包含缺少值的行之間的距離。我試圖排除我不需要的組合。不幸的是,我的解決方案需要更多時間:
# Some example data
data <- data.frame(
x1 = c(1, 22, NA, NA, 15, 7, 10, 8, NA, 5),
x2 = c(11, 2, 7, 15, 1, 17, 11, 18, 5, 5),
x3 = c(21, 5, 6, NA, 10, 22, 12, 2, 12, 3),
x4 = c(13, NA, NA, 20, 12, 5, 1, 8, 7, 14)
)
# Measure speed of dist() function
start_time_dist <- Sys.time()
# Calculate euclidean distance with dist() function for complete dataset
dist_results <- dist(data)
end_time_dist <- Sys.time()
time_taken_dist <- end_time_dist - start_time_dist
# Measure speed of my own loop
start_time_own <- Sys.time()
# Calculate euclidean distance with my own loop only for specific cases
# # #
# The following code should be faster!
# # #
data_cc <- data[complete.cases(data), ]
data_miss <- data[complete.cases(data) == FALSE, ]
distance_list <- list()
for(i in 1:nrow(data_miss)) {
distances <- numeric()
for(j in 1:nrow(data_cc)) {
distances <- c(distances, dist(rbind(data_miss[i, ], data_cc[j, ]), method = "euclidean"))
}
distance_list[[i]] <- distances
}
end_time_own <- Sys.time()
time_taken_own <- end_time_own - start_time_own
# Compare speed of both calculations
time_taken_dist # 0.002001047 secs
time_taken_own # 0.01562881 secs
有沒有更快的方式來計算我需要的歐氏距離?非常感謝!
dist在C中實現,當然它比R for循環更快。你應該在Rcpp中實現你的循環。 – Roland
謝謝你的提示!我會試着弄清楚它是如何工作的。 – JSP