r-使用purrr和dplyr将函数应用于列的子集



我是purrr软件包的新手,但我喜欢对它所知甚少。

仅使用tidyverse包,我希望能够添加一列,该列是应用于数据集中列子集的函数的结果。

这是一些玩具数据。因子的一系列列

df <- data.frame(a_1 = factor(rep(letters[1:3], times = 5)),
a_2 = factor(rep(letters[1:3], times = 5)),
a_3 = factor(rep(letters[1:3], times = 5)),
b_1 = factor(rep(letters[1:3], times = 5)),
b_2 = factor(rep(letters[1:3], times = 5)),
b_3 = factor(rep(letters[1:3], times = 5)))
df
# output
#  a_1 a_2 a_3 b_1 b_2 b_3
# 1    a   a   a   a   a   a
# 2    b   b   b   b   b   b
# 3    c   c   c   c   c   c
# 4    a   a   a   a   a   a
# 5    b   b   b   b   b   b
# 6    c   c   c   c   c   c
# 7    a   a   a   a   a   a
# 8    b   b   b   b   b   b
# 9    c   c   c   c   c   c
# 10   a   a   a   a   a   a
# 11   b   b   b   b   b   b
# 12   c   c   c   c   c   c
# 13   a   a   a   a   a   a
# 14   b   b   b   b   b   b
# 15   c   c   c   c   c   c

以下函数通过purr::map_dfdplyr::select循环遍历以a_开头的df列,将它们转换为数值类,找到这些列的平均值,然后乘以3。

rowMeans(purrr::map_df(.x = df %>% dplyr::select(grep("a_", names(.))),
.f = function(x) x <- as.numeric(x))*3)
# output
# [1] 3 6 9 3 6 9 3 6 9 3 6 9 3 6 9

这是正确的输出,但却是一个矢量。

使用tidyverse函数,如何将函数的结果作为新列而不是矢量添加到现有的df数据集

我想是dplyr::mutate的问题,但我无法解决。

您可以使用pmap_dbl:

library(dplyr)
library(purrr)
df %>%  
mutate(mean_vec = pmap_dbl(select(., starts_with('a_')), 
~mean(as.numeric(c(...)) * 3)))

#   a_1 a_2 a_3 b_1 b_2 b_3 mean_vec
#1    1   1   1   a   a   a        3
#2    2   2   2   b   b   b        6
#3    3   3   3   c   c   c        9
#4    1   1   1   a   a   a        3
#5    2   2   2   b   b   b        6
#6    3   3   3   c   c   c        9
#7    1   1   1   a   a   a        3
#8    2   2   2   b   b   b        6
#9    3   3   3   c   c   c        9
#10   1   1   1   a   a   a        3
#11   2   2   2   b   b   b        6
#12   3   3   3   c   c   c        9
#13   1   1   1   a   a   a        3
#14   2   2   2   b   b   b        6
#15   3   3   3   c   c   c        9

或者另一种选择:

df %>%
mutate_at(vars(starts_with('a')), as.numeric) %>%
mutate(mean_vec = rowMeans(select(., starts_with('a_')) * 3))

最新更新