在 R 中自动化(循环)欧氏距离测量
Automating (loops) Euclidean distance measurements in R
AIM: 我想自动执行(循环)下面的代码,而不必为每个示例手动 运行 它。我有一个很糟糕的习惯,在 base 中写长手,并且需要开始使用循环,我发现这很难实现。
DATA: 我有两个数据框:一个样本数据(样本)和一个参考数据(ref)。
它们都包含相同的变量 (x, y, z).
代码说明: 对于每个样本 (sample$sample_name),我想计算它与参考数据中每个案例的欧氏距离。然后使用结果对参考数据重新排序,以显示欧几里德(3 维)space.
中哪些点 'closest' 到样本数据点
我当前的代码允许我简单地替换示例名称(即“s1”),然后重新 运行 代码,最后更改 .csv 文件的文件名。输出是参考数据列表,按照最接近样本的顺序排列(欧几里得 space)。
我想自动执行该过程(进入循环?),这样我就可以使用样本名称列表 (samples$sample_name) 在两个数据框中简单地 运行 它,并希望还可以自动导出到 .csv 文件。
如有任何帮助,我们将不胜感激!
# Reference data
country<-c("Austria","Austria","Italy","Italy","Turkey","Romania","France")
x<-c(18.881,18.881,18.929,19.139,19.008,19.083,18.883)
y<-c(15.627,15.627,15.654,15.772,15.699,15.741,15.629)
z<-c(38.597,38.597,38.842,39.409,39.048,39.224,38.740)
pb_age<-c(-106,-106,-87,-6,-55,-26,-104)
ref<-data.frame(country,x,y,z,pb_age) # Reference data
# Sample data (for euclidean measurements against Reference data)
sample_name<-c("s1","s2","s3")
x2<-c(18.694,18.729,18.731)
y2<-c(15.682,15.683,15.677)
z2<-c(38.883,38.989,38.891)
pb_age2<-c(120,97,82)
samples<-data.frame(sample_name,x2,y2,z2,pb_age2) # Sample data
colnames(samples)<-c("sample_name","x","y","z","pb_age") # To match Reference data headings
# Euclidean distance measurements
library(fields) # Need package for Euclidean distances
# THIS IS WHAT I WANT TO AUTOMATE/LOOP (BELOW)...
# Currently, I have to update the 'id' for each sample to get a result (for each sample)
id<-"s1" # Sample ID - this is simply changed so the following code can be re-run for each sample
# The code
x1<-samples[which(samples$sample_name==id),c("x","y","z")]
x2<-ref[,c("x","y","z")]
result_distance<-rdist(x1,x2) # Computing the Euclidean distance
result_distance<-as.vector(result_distance) # Saving the results as a vector
euclid_ref<-data.frame(result_distance,ref) # Creating a new data.frame adding the Euclidean distances to the original Reference data
colnames(euclid_ref)[1]<-"euclid_distance" # Updating the column name for the result
# Saving and exporting the results
results<-euclid_ref[order(euclid_ref$euclid_distance),] # Re-ordering the data.frame by the euclide distances, smallest to largest
write.csv(results, file="s1.csv") # Ideally, I want the file name to be the same as the SAMPLE id, i.e. s1, s2, s3...
一个循环就足够简单了,但是更像 R 的解决方案是利用向量化和应用函数族:
result_distances <- data.frame(t(rdist(samples[, 2:4], ref[, 2:4])), ref)
colnames(result_distances)[1:3] <- rep("euclid_distance", 3)
# str(result_distances)
# 'data.frame': 7 obs. of 8 variables:
# $ euclid_distance: num 0.346 0.346 0.24 0.695 0.355 ...
# $ euclid_distance: num 0.424 0.424 0.25 0.594 0.286 ...
# $ euclid_distance: num 0.334 0.334 0.205 0.666 0.319 ...
# $ country : chr "Austria" "Austria" "Italy" "Italy" ...
# $ x : num 18.9 18.9 18.9 19.1 19 ...
# $ y : num 15.6 15.6 15.7 15.8 15.7 ...
# $ z : num 38.6 38.6 38.8 39.4 39 ...
# $ pb_age : num -106 -106 -87 -6 -55 -26 -104
通常我们不会给多个列起相同的名字,但我们打算接下来将它们拉出来:
results <- lapply(1:3, function(i) data.frame(result_distances[order(result_distances[, i]), c(i, 4:8)]))
names(results) <- samples$sample_name
现在我们有一个名为 results
的列表,其中包含名为“s1”、“s2”和“s3”的三个数据框。列表使将函数应用于许多组织相似的数据集变得容易。例如 results[["s1"]]
或 results[[1]]
打印第一个样本的数据框。现在我们把结果写出来:
sapply(names(results), function(x) write.csv(results[[x]], file=paste0(x, ".csv")))
这将创建 3 个文件,“s1.csv”、“s2.csv”、“s3.csv”。
这是一个循环,使用您的原始输入数据和代码的关键部分计算所有样本与参考数据位置的欧氏距离。它比 vectorised-apply 解决方案更冗长,但可能更容易阅读,因为它不那么简洁和嵌套。最终输出是单个数据框。
# prepare an empty list object to store the results
output <- vector("list", length = nrow(samples))
# this is the start of the loop
for(i in seq_len(nrow(samples))){
# we can read this as 'for row i of the samples dataframe, do this...'
# get coords for sample i
sample_coords <- samples[i ,c("x","y","z")]
# get coords for all reference locations
# this line would be fine above the loop
# since it gives the same result for each
# iteration. I place it here to echo your
# original workflow
ref_coords <- ref[,c("x","y","z")]
# compute Euclidean distance and coerce to vector,
e_dist_vec <- as.vector(rdist(sample_coords, ref_coords))
# store in data frame
e_dist_ref_df <- data.frame(e_dist_vec, ref)
# update colname
colnames(e_dist_ref_df)[1] <- "euclid_distance"
# order df by euclid_distance values
results <- e_dist_ref_df[order(e_dist_ref_df$euclid_distance),]
# store results for sample i in the list
output[[i]] <- results
} # this is the end of the loop
# assign sample names to list items
names(output) <- samples$sample_name
此时我们有一个数据框列表(每个样本一个),您可以将其写入单独的 CSV(就像我们在@dcarlson 的回答中看到的那样),每个数据框一个文件,或者我们可以继续放置它们都在一个数据框中用于下游分析等。这是循环输出列表的样子:
> output
$s1
euclid_distance country x y z pb_age
3 0.2401874 Italy 18.929 15.654 38.842 -87
7 0.2428559 France 18.883 15.629 38.740 -104
1 0.3461069 Austria 18.881 15.627 38.597 -106
2 0.3461069 Austria 18.881 15.627 38.597 -106
5 0.3551197 Turkey 19.008 15.699 39.048 -55
6 0.5206563 Romania 19.083 15.741 39.224 -26
4 0.6948388 Italy 19.139 15.772 39.409 -6
$s2
euclid_distance country x y z pb_age
3 0.2499000 Italy 18.929 15.654 38.842 -87
5 0.2856186 Turkey 19.008 15.699 39.048 -55
7 0.2977129 France 18.883 15.629 38.740 -104
1 0.4241509 Austria 18.881 15.627 38.597 -106
2 0.4241509 Austria 18.881 15.627 38.597 -106
6 0.4288415 Romania 19.083 15.741 39.224 -26
4 0.5936506 Italy 19.139 15.772 39.409 -6
$s3
euclid_distance country x y z pb_age
3 0.2052657 Italy 18.929 15.654 38.842 -87
7 0.2195655 France 18.883 15.629 38.740 -104
5 0.3191583 Turkey 19.008 15.699 39.048 -55
1 0.3338203 Austria 18.881 15.627 38.597 -106
2 0.3338203 Austria 18.881 15.627 38.597 -106
6 0.4887627 Romania 19.083 15.741 39.224 -26
4 0.6661929 Italy 19.139 15.772 39.409 -6
通常将它放在一个数据框中进行进一步分析会很方便,这是一种方法:
# bind list dfs into one big data frame, not sure what the one-line equivalent in base R is
output_df <- dplyr::bind_rows(output, .id = "sample_id")
最终产品的外观如下:
> output_df
sample_id euclid_distance country x y z pb_age
1 s1 0.2401874 Italy 18.929 15.654 38.842 -87
2 s1 0.2428559 France 18.883 15.629 38.740 -104
3 s1 0.3461069 Austria 18.881 15.627 38.597 -106
4 s1 0.3461069 Austria 18.881 15.627 38.597 -106
5 s1 0.3551197 Turkey 19.008 15.699 39.048 -55
6 s1 0.5206563 Romania 19.083 15.741 39.224 -26
7 s1 0.6948388 Italy 19.139 15.772 39.409 -6
8 s2 0.2499000 Italy 18.929 15.654 38.842 -87
9 s2 0.2856186 Turkey 19.008 15.699 39.048 -55
10 s2 0.2977129 France 18.883 15.629 38.740 -104
11 s2 0.4241509 Austria 18.881 15.627 38.597 -106
12 s2 0.4241509 Austria 18.881 15.627 38.597 -106
13 s2 0.4288415 Romania 19.083 15.741 39.224 -26
14 s2 0.5936506 Italy 19.139 15.772 39.409 -6
15 s3 0.2052657 Italy 18.929 15.654 38.842 -87
16 s3 0.2195655 France 18.883 15.629 38.740 -104
17 s3 0.3191583 Turkey 19.008 15.699 39.048 -55
18 s3 0.3338203 Austria 18.881 15.627 38.597 -106
19 s3 0.3338203 Austria 18.881 15.627 38.597 -106
20 s3 0.4887627 Romania 19.083 15.741 39.224 -26
21 s3 0.6661929 Italy 19.139 15.772 39.409 -6
AIM: 我想自动执行(循环)下面的代码,而不必为每个示例手动 运行 它。我有一个很糟糕的习惯,在 base 中写长手,并且需要开始使用循环,我发现这很难实现。
DATA: 我有两个数据框:一个样本数据(样本)和一个参考数据(ref)。 它们都包含相同的变量 (x, y, z).
代码说明: 对于每个样本 (sample$sample_name),我想计算它与参考数据中每个案例的欧氏距离。然后使用结果对参考数据重新排序,以显示欧几里德(3 维)space.
中哪些点 'closest' 到样本数据点我当前的代码允许我简单地替换示例名称(即“s1”),然后重新 运行 代码,最后更改 .csv 文件的文件名。输出是参考数据列表,按照最接近样本的顺序排列(欧几里得 space)。
我想自动执行该过程(进入循环?),这样我就可以使用样本名称列表 (samples$sample_name) 在两个数据框中简单地 运行 它,并希望还可以自动导出到 .csv 文件。
如有任何帮助,我们将不胜感激!
# Reference data
country<-c("Austria","Austria","Italy","Italy","Turkey","Romania","France")
x<-c(18.881,18.881,18.929,19.139,19.008,19.083,18.883)
y<-c(15.627,15.627,15.654,15.772,15.699,15.741,15.629)
z<-c(38.597,38.597,38.842,39.409,39.048,39.224,38.740)
pb_age<-c(-106,-106,-87,-6,-55,-26,-104)
ref<-data.frame(country,x,y,z,pb_age) # Reference data
# Sample data (for euclidean measurements against Reference data)
sample_name<-c("s1","s2","s3")
x2<-c(18.694,18.729,18.731)
y2<-c(15.682,15.683,15.677)
z2<-c(38.883,38.989,38.891)
pb_age2<-c(120,97,82)
samples<-data.frame(sample_name,x2,y2,z2,pb_age2) # Sample data
colnames(samples)<-c("sample_name","x","y","z","pb_age") # To match Reference data headings
# Euclidean distance measurements
library(fields) # Need package for Euclidean distances
# THIS IS WHAT I WANT TO AUTOMATE/LOOP (BELOW)...
# Currently, I have to update the 'id' for each sample to get a result (for each sample)
id<-"s1" # Sample ID - this is simply changed so the following code can be re-run for each sample
# The code
x1<-samples[which(samples$sample_name==id),c("x","y","z")]
x2<-ref[,c("x","y","z")]
result_distance<-rdist(x1,x2) # Computing the Euclidean distance
result_distance<-as.vector(result_distance) # Saving the results as a vector
euclid_ref<-data.frame(result_distance,ref) # Creating a new data.frame adding the Euclidean distances to the original Reference data
colnames(euclid_ref)[1]<-"euclid_distance" # Updating the column name for the result
# Saving and exporting the results
results<-euclid_ref[order(euclid_ref$euclid_distance),] # Re-ordering the data.frame by the euclide distances, smallest to largest
write.csv(results, file="s1.csv") # Ideally, I want the file name to be the same as the SAMPLE id, i.e. s1, s2, s3...
一个循环就足够简单了,但是更像 R 的解决方案是利用向量化和应用函数族:
result_distances <- data.frame(t(rdist(samples[, 2:4], ref[, 2:4])), ref)
colnames(result_distances)[1:3] <- rep("euclid_distance", 3)
# str(result_distances)
# 'data.frame': 7 obs. of 8 variables:
# $ euclid_distance: num 0.346 0.346 0.24 0.695 0.355 ...
# $ euclid_distance: num 0.424 0.424 0.25 0.594 0.286 ...
# $ euclid_distance: num 0.334 0.334 0.205 0.666 0.319 ...
# $ country : chr "Austria" "Austria" "Italy" "Italy" ...
# $ x : num 18.9 18.9 18.9 19.1 19 ...
# $ y : num 15.6 15.6 15.7 15.8 15.7 ...
# $ z : num 38.6 38.6 38.8 39.4 39 ...
# $ pb_age : num -106 -106 -87 -6 -55 -26 -104
通常我们不会给多个列起相同的名字,但我们打算接下来将它们拉出来:
results <- lapply(1:3, function(i) data.frame(result_distances[order(result_distances[, i]), c(i, 4:8)]))
names(results) <- samples$sample_name
现在我们有一个名为 results
的列表,其中包含名为“s1”、“s2”和“s3”的三个数据框。列表使将函数应用于许多组织相似的数据集变得容易。例如 results[["s1"]]
或 results[[1]]
打印第一个样本的数据框。现在我们把结果写出来:
sapply(names(results), function(x) write.csv(results[[x]], file=paste0(x, ".csv")))
这将创建 3 个文件,“s1.csv”、“s2.csv”、“s3.csv”。
这是一个循环,使用您的原始输入数据和代码的关键部分计算所有样本与参考数据位置的欧氏距离。它比 vectorised-apply 解决方案更冗长,但可能更容易阅读,因为它不那么简洁和嵌套。最终输出是单个数据框。
# prepare an empty list object to store the results
output <- vector("list", length = nrow(samples))
# this is the start of the loop
for(i in seq_len(nrow(samples))){
# we can read this as 'for row i of the samples dataframe, do this...'
# get coords for sample i
sample_coords <- samples[i ,c("x","y","z")]
# get coords for all reference locations
# this line would be fine above the loop
# since it gives the same result for each
# iteration. I place it here to echo your
# original workflow
ref_coords <- ref[,c("x","y","z")]
# compute Euclidean distance and coerce to vector,
e_dist_vec <- as.vector(rdist(sample_coords, ref_coords))
# store in data frame
e_dist_ref_df <- data.frame(e_dist_vec, ref)
# update colname
colnames(e_dist_ref_df)[1] <- "euclid_distance"
# order df by euclid_distance values
results <- e_dist_ref_df[order(e_dist_ref_df$euclid_distance),]
# store results for sample i in the list
output[[i]] <- results
} # this is the end of the loop
# assign sample names to list items
names(output) <- samples$sample_name
此时我们有一个数据框列表(每个样本一个),您可以将其写入单独的 CSV(就像我们在@dcarlson 的回答中看到的那样),每个数据框一个文件,或者我们可以继续放置它们都在一个数据框中用于下游分析等。这是循环输出列表的样子:
> output
$s1
euclid_distance country x y z pb_age
3 0.2401874 Italy 18.929 15.654 38.842 -87
7 0.2428559 France 18.883 15.629 38.740 -104
1 0.3461069 Austria 18.881 15.627 38.597 -106
2 0.3461069 Austria 18.881 15.627 38.597 -106
5 0.3551197 Turkey 19.008 15.699 39.048 -55
6 0.5206563 Romania 19.083 15.741 39.224 -26
4 0.6948388 Italy 19.139 15.772 39.409 -6
$s2
euclid_distance country x y z pb_age
3 0.2499000 Italy 18.929 15.654 38.842 -87
5 0.2856186 Turkey 19.008 15.699 39.048 -55
7 0.2977129 France 18.883 15.629 38.740 -104
1 0.4241509 Austria 18.881 15.627 38.597 -106
2 0.4241509 Austria 18.881 15.627 38.597 -106
6 0.4288415 Romania 19.083 15.741 39.224 -26
4 0.5936506 Italy 19.139 15.772 39.409 -6
$s3
euclid_distance country x y z pb_age
3 0.2052657 Italy 18.929 15.654 38.842 -87
7 0.2195655 France 18.883 15.629 38.740 -104
5 0.3191583 Turkey 19.008 15.699 39.048 -55
1 0.3338203 Austria 18.881 15.627 38.597 -106
2 0.3338203 Austria 18.881 15.627 38.597 -106
6 0.4887627 Romania 19.083 15.741 39.224 -26
4 0.6661929 Italy 19.139 15.772 39.409 -6
通常将它放在一个数据框中进行进一步分析会很方便,这是一种方法:
# bind list dfs into one big data frame, not sure what the one-line equivalent in base R is
output_df <- dplyr::bind_rows(output, .id = "sample_id")
最终产品的外观如下:
> output_df
sample_id euclid_distance country x y z pb_age
1 s1 0.2401874 Italy 18.929 15.654 38.842 -87
2 s1 0.2428559 France 18.883 15.629 38.740 -104
3 s1 0.3461069 Austria 18.881 15.627 38.597 -106
4 s1 0.3461069 Austria 18.881 15.627 38.597 -106
5 s1 0.3551197 Turkey 19.008 15.699 39.048 -55
6 s1 0.5206563 Romania 19.083 15.741 39.224 -26
7 s1 0.6948388 Italy 19.139 15.772 39.409 -6
8 s2 0.2499000 Italy 18.929 15.654 38.842 -87
9 s2 0.2856186 Turkey 19.008 15.699 39.048 -55
10 s2 0.2977129 France 18.883 15.629 38.740 -104
11 s2 0.4241509 Austria 18.881 15.627 38.597 -106
12 s2 0.4241509 Austria 18.881 15.627 38.597 -106
13 s2 0.4288415 Romania 19.083 15.741 39.224 -26
14 s2 0.5936506 Italy 19.139 15.772 39.409 -6
15 s3 0.2052657 Italy 18.929 15.654 38.842 -87
16 s3 0.2195655 France 18.883 15.629 38.740 -104
17 s3 0.3191583 Turkey 19.008 15.699 39.048 -55
18 s3 0.3338203 Austria 18.881 15.627 38.597 -106
19 s3 0.3338203 Austria 18.881 15.627 38.597 -106
20 s3 0.4887627 Romania 19.083 15.741 39.224 -26
21 s3 0.6661929 Italy 19.139 15.772 39.409 -6