[英]how to calculate Euclidean distance between all rows of a data frame and the last row in r
I have a data frame and would like to calculate the Euclidean distance between all rows and the last row and add the distance value as a new column to data frame using distance function. 我有一个数据框,想计算所有行和最后一行之间的欧几里得距离,并使用距离函数将距离值作为新列添加到数据框。
Do you have any idea how can I do this. 你有什么想法我该怎么做。
here is an example of data frame: 这是数据帧的示例:
df = data.frame(
x = rnorm(10),
y = rnorm(10),
z = rnorm(10)
)
This is the desired output 这是所需的输出
x y z dist
1 2.4720136 -2.5332449 -0.29877255 1.956157
2 0.2616905 -0.5988683 -0.68586911 3.434874
3 0.4706199 -0.7911288 -0.07673025 2.700038
4 0.7202775 -2.0615370 -0.93959256 2.493054
5 -0.3002038 1.5872991 1.43678171 2.310108
6 -2.1274192 1.9746993 0.55372197 4.39925
7 0.2568586 0.2206467 0.22742260 3.482363
8 1.3476458 -0.2029571 -0.98492886 1.459337
9 -1.1968263 -0.4381387 0.93461394 2.180699
10 -0.2995206 -1.6586264 -0.25067014 NA
You can use dist
to find the distances between each row of the dataframe, eg: 您可以使用dist
查找数据帧每一行之间的距离,例如:
df = data.frame(
x = rnorm(10),
y = rnorm(10),
z = rnorm(10)
)
# This will generate all the pairwise differences
# Might be an issue of very large datasets
# where speed will be an issue
df$dist = as.matrix(dist(df))[nrow(df), ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.