我想计算具有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