簡體   English   中英

使用purrr(tidyverse)在數據框的所有列上映射距離函數

[英]Using purrr (tidyverse) to map distance function across all columns of dataframe

我有一個距離函數,該函數接受2個(數字)向量並計算它們之間的距離。

對於以下示例中的給定數據幀( 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的每一列上返回向量嗎?

使用map_dbl ,這是map一種特殊情況,它可以遍歷列,但顯式地返回double類型的vector:

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM