使用 purrr (tidyverse) 在数据框的所有列之间映射距离函数 [英] Using purrr (tidyverse) to map distance function across all columns of dataframe

查看:60
本文介绍了使用 purrr (tidyverse) 在数据框的所有列之间映射距离函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个距离函数,它接收 2 个(数字)向量并计算它们之间的距离.

I have a distance function which takes in 2 (numeric) vectors and calculates the distance between them.

对于下面示例中的给定数据帧(mtcars_raw)和固定输入向量(test_vec),我想计算成对距离(即应用距离函数) 到每一列和 test_vec 并返回距离向量.向量的长度应该是列数.

For a given dataframe (mtcars_raw) in the example below and a fixed input vector (test_vec) I would like to calculate the pairwise distances (i.e. apply the distance function) to each column and test_vec and return the vector of distances. The length of the vector should be the number of columns.

请查看可重现的示例:


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) 代码以在 mtcars_raw 的每一列上针对 test_vec 返回向量吗?

Could anyone please show the purrr (tidyverse) code to return vector on each column of mtcars_raw against test_vec?

推荐答案

使用 map_dbl,它是 map 的一个特例,用于遍历列但显式返回 double 类型向量:

Use map_dbl, which is a special case of map to loop through columns but explicitly return double type 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 

这篇关于使用 purrr (tidyverse) 在数据框的所有列之间映射距离函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆