![](/img/trans.png)
[英]Using purrr (tidyverse) to map distance function across all columns of dataframe
[英]Purrr map a function to all columns except for one
我想将 Blom 转换应用于除“ID”之外的所有列。 由于它们都是数字, map_if
和is.numeric
在这里不起作用。
library(rcompanion)
data("mtcars")
# Get a list of the columns that I would like to apply the Blom transformation
col_names = colnames(mtcars)
# Create the column ID so each row has a unique identifier
mtcars = mtcars %>% mutate(id = row_number())
我想出的是:
mtcars = map_if(mtcars, names(.) %in% col_names ~ rcompanion::blom(.x, method = "blom")
但是我收到一个错误: ! Can't convert `.f`, a two-sided formula, to a function.
! Can't convert `.f`, a two-sided formula, to a function.
如果我们使用cross,这可以across
dplyr
内部完成
library(dplyr)
mtcars <- mtcars %>%
mutate(across(all_of(col_names), rcompanion::blom, method = "blom"))
-输出
> head(mtcars)
mpg cyl disp hp drat wt qsec vs am gear
Mazda RX4 0.2353289 -0.1168527 -0.23532887 -0.2754823 0.3989603 -0.62056827 -0.9368471 -0.5741128 0.8223940 0.3989603
Mazda RX4 Wag 0.2353289 -0.1168527 -0.23532887 -0.2754823 0.3989603 -0.35721583 -0.4846749 -0.5741128 0.8223940 0.3989603
Datsun 710 0.6684036 -0.9368471 -0.93684711 -0.8223940 0.2754823 -0.82239396 0.5288648 0.7690553 0.8223940 0.3989603
Hornet 4 Drive 0.3989603 -0.1168527 0.11685275 -0.2754823 -0.6684036 -0.03887224 0.8223940 0.7690553 -0.5288648 -0.7178201
Hornet Sportabout -0.1168527 0.7690553 0.99892823 0.3572158 -0.4846749 0.19555148 -0.4846749 -0.5741128 -0.5288648 -0.7178201
Valiant -0.1955515 -0.1168527 0.03887224 -0.5288648 -1.8134176 0.35721583 1.6411071 0.7690553 -0.5288648 -0.7178201
carb id
Mazda RX4 0.7690553 1
Mazda RX4 Wag 0.7690553 2
Datsun 710 -1.2138469 3
Hornet 4 Drive -1.2138469 4
Hornet Sportabout -0.3160850 5
Valiant -1.2138469 6
使用purrr::map_at
你可以:
library(rcompanion)
library(purrr)
library(dplyr)
mtcars[] <- map_at(mtcars, col_names, ~ rcompanion::blom(.x, method = "blom"))
head(mtcars)
#> mpg cyl disp hp drat
#> Mazda RX4 0.2353289 -0.1168527 -0.23532887 -0.2754823 0.3989603
#> Mazda RX4 Wag 0.2353289 -0.1168527 -0.23532887 -0.2754823 0.3989603
#> Datsun 710 0.6684036 -0.9368471 -0.93684711 -0.8223940 0.2754823
#> Hornet 4 Drive 0.3989603 -0.1168527 0.11685275 -0.2754823 -0.6684036
#> Hornet Sportabout -0.1168527 0.7690553 0.99892823 0.3572158 -0.4846749
#> Valiant -0.1955515 -0.1168527 0.03887224 -0.5288648 -1.8134176
#> wt qsec vs am gear
#> Mazda RX4 -0.62056827 -0.9368471 -0.5741128 0.8223940 0.3989603
#> Mazda RX4 Wag -0.35721583 -0.4846749 -0.5741128 0.8223940 0.3989603
#> Datsun 710 -0.82239396 0.5288648 0.7690553 0.8223940 0.3989603
#> Hornet 4 Drive -0.03887224 0.8223940 0.7690553 -0.5288648 -0.7178201
#> Hornet Sportabout 0.19555148 -0.4846749 -0.5741128 -0.5288648 -0.7178201
#> Valiant 0.35721583 1.6411071 0.7690553 -0.5288648 -0.7178201
#> carb id
#> Mazda RX4 0.7690553 1
#> Mazda RX4 Wag 0.7690553 2
#> Datsun 710 -1.2138469 3
#> Hornet 4 Drive -1.2138469 4
#> Hornet Sportabout -0.3160850 5
#> Valiant -1.2138469 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.