2017-10-10 46 views
0

我有一個距離函數,它需要2個(數值)向量並計算它們之間的距離。使用purrr(tidyverse)映射跨所有數據幀的距離函數

對於下面的例子中的給定數據幀(mtcars_raw)和一個固定的輸入向量(test_vec)我想以計算成對距離(即申請的距離函數),以每列和test_vec並返回距離的矢量。矢量的長度應該是列的數量。

請參閱重複的例子:


library(datasets) 

# The raw dataframe containing only numeric columns 
mtcars_raw <- datasets::mtcars 

# The distance function between 2 vectors (of the same length typically) 
eucl_dist <- function(x, y){ 
    return(sqrt(sum((x-y)^2))) 
} 

# An example of a numeric vector to check the distance against each column 
test_vec <- rnorm(n = dim(mtcars_raw)[1], mean = 12, sd = 2) 

# Manually for the first column, we would have: 
dist_1 <- eucl_dist(x = test_vec, mtcars_raw[, 1]) 
dist_1 
#> [1] 58.71256 

# Manually for the second column, we would have: 
dist_2 <- eucl_dist(x = test_vec, mtcars_raw[, 1]) 
dist_2 
#> [1] 58.71256 

# Would like dist_comb returned for all columns without having to manually do 
# the combining 
dist_comb <- c(dist_1, dist_2) 
dist_comb 
#> [1] 58.71256 58.71256 

任何人都可以請出示purrr (tidyverse)代碼關於對test_vec mtcars_raw的每列返回向量?

回答

1

使用map_dbl,這是map一個特例來遍歷列,但明確地返回double型向量:

map_dbl(mtcars_raw[1:2], ~ eucl_dist(test_vec, .x)) 

#  mpg  cyl 
#58.06386 36.51686 

所有列:

map_dbl(mtcars_raw, ~ eucl_dist(test_vec, .x)) 

#  mpg  cyl  disp   hp  drat   wt  qsec   vs   am  gear  carb 
# 58.06386 36.51686 1414.98943 850.71261 49.72837 51.74005 35.50658 67.25079 67.35504 49.34896 54.56577 
+1

謝謝你 - 這正是我需要的 – user4687531