我们可以使用dplyr::across,这里是一个例子:
library(dplyr)
library(rcompanion)
# from `?blom`'s examples
Value = c(709,679,699,657,594,677,592,538,476,508,505,539)
Sex = c(rep("Male",3), rep("Female",3), rep("Male",3), rep("Female",3))
Fat = c(rep("Fresh", 6), rep("Rancid", 6))
Sokal = data.frame(Value, Sex, Fat)
# dplyr::across with the example above (one varialbe)
Sokal %>%
mutate(across(Value, list(BLR = ~ blom(.x, method = "blom"))))
#> Value Sex Fat Value_BLR
#> 1 709 Male Fresh 1.6350393
#> 2 679 Male Fresh 0.7916386
#> 3 699 Male Fresh 1.1139372
#> 4 657 Female Fresh 0.3119191
#> 5 594 Female Fresh 0.1024905
#> 6 677 Female Fresh 0.5361763
#> 7 592 Male Rancid -0.1024905
#> 8 538 Male Rancid -0.5361763
#> 9 476 Male Rancid -1.6350393
#> 10 508 Female Rancid -0.7916386
#> 11 505 Female Rancid -1.1139372
#> 12 539 Female Rancid -0.3119191
# applying this example to your data.frame
your_df <- setNames(as.list(1:8), LETTERS[18:25]) %>% as_tibble
# using tidyselect `matches()` with a regex in `across`
your_df %>%
mutate(across(matches("[R-Y]"),
list(BLR = ~ blom(.x, method = "blom"))))
# or select variables by position in `across`
your_df %>%
mutate(across(1:7,
list(BLR = ~ blom(.x, method = "blom"))))
由reprex package (v0.3.0) 于 2021-07-24 创建