如何在R的管道序列中的选定列上应用函数?

时间:2019-04-23 08:53:53

标签: r dplyr tidyverse magrittr

我有一个包含许多列的数据框(或者不管是小标题),我只想对其中的7个应用一个函数(比如rowSums),但是我不想弄糟其他的。诀窍是我想按管道顺序执行此操作 -创建(或读取数据) -应用功能 -之后的可选操作

这是我要在前三列上进行求和的数据框上的可复制示例。

data <- data.frame("v1" = runif(10, 0, 10), "v2" = runif(10, 0 ,10), "v3" = runif(10, 0 ,10), "v4" = rep("some_charchter", 10))

我通常会做的方式是

data$sum <- rowSums(data[,1:3])

但是我想要这样的东西

data <- data.frame("v1" = runif(10, 0, 10), "v2" = runif(10, 0 ,10), "v3" = runif(10, 0 ,10), "v4" = rep("some_charchter", 10)) %>% 
  mutate(sum = rowSums())

感谢您的帮助!

3 个答案:

答案 0 :(得分:2)

您可以使用.在管道内访问数据对象。因此mutate(sum = rowSums(.[, 1:3]))可以达到目的:

data <- data.frame("v1" = runif(10, 0, 10), "v2" = runif(10, 0 ,10), "v3" = runif(10, 0 ,10), "v4" = rep("some_charchter", 10)) %>% 
  mutate(sum = rowSums(.[, 1:3]))

data
         v1        v2        v3             v4       sum
1  2.280871 0.1981815 7.5349128 some_charchter 10.013965
2  1.250208 7.6687056 0.6193483 some_charchter  9.538262
3  6.782954 3.6973201 2.7694021 some_charchter 13.249677
4  3.809574 6.8641731 3.1271489 some_charchter 13.800896
5  9.339726 4.4571677 5.4489081 some_charchter 19.245802
6  6.623371 3.9594287 0.6025072 some_charchter 11.185307
7  6.843193 1.3548732 3.1826649 some_charchter 11.380731
8  2.377099 7.5661778 9.6320561 some_charchter 19.575333
9  3.582874 2.1485691 8.2970807 some_charchter 14.028524
10 4.565336 3.7073800 0.3355328 some_charchter  8.608248

答案 1 :(得分:1)

另一种选择是根据您在选定列上的返回类型使用pmap_*的变体形式。

library(dplyr)
library(purrr)

data %>%  mutate(sum = pmap_dbl(list(v1, v2, v3), sum))


#           v1       v2        v3             v4       sum
#1  1.13703411 6.935913 3.1661245 some_charchter 11.239072
#2  6.22299405 5.449748 3.0269337 some_charchter 14.699676
#3  6.09274733 2.827336 1.5904600 some_charchter 10.510543
#4  6.23379442 9.234335 0.3999592 some_charchter 15.868088
#5  8.60915384 2.923158 2.1879954 some_charchter 13.720308
#6  6.40310605 8.372956 8.1059855 some_charchter 22.882048
#7  0.09495756 2.862233 5.2569755 some_charchter  8.214166
#8  2.32550506 2.668208 9.1465817 some_charchter 14.140295
#9  6.66083758 1.867228 8.3134505 some_charchter 16.841516
#10 5.14251141 2.322259 0.4577026 some_charchter  7.922473

数据

set.seed(1234)
data <- data.frame("v1" = runif(10, 0, 10), "v2" = runif(10, 0 ,10), 
                   "v3" = runif(10, 0 ,10), "v4" = rep("some_charchter", 10))

答案 2 :(得分:1)

我们可能还需要进行轻微的类型检查,以使流程良好地自动化

library(tidyverse)
data %>%
       mutate(Sum =  select_if(., is.numeric) %>% 
                                           reduce(`+`))
#        v1       v2        v3             v4       Sum
#1  1.13703411 6.935913 3.1661245 some_charchter 11.239072
#2  6.22299405 5.449748 3.0269337 some_charchter 14.699676
#3  6.09274733 2.827336 1.5904600 some_charchter 10.510543
#4  6.23379442 9.234335 0.3999592 some_charchter 15.868088
#5  8.60915384 2.923158 2.1879954 some_charchter 13.720308
#6  6.40310605 8.372956 8.1059855 some_charchter 22.882048
#7  0.09495756 2.862233 5.2569755 some_charchter  8.214166
#8  2.32550506 2.668208 9.1465817 some_charchter 14.140295
#9  6.66083758 1.867228 8.3134505 some_charchter 16.841516
#10 5.14251141 2.322259 0.4577026 some_charchter  7.922473

注意:这将是矢量化解决方案,类似于@symbolrush的rowSums解决方案

数据

set.seed(1234)
data <- data.frame("v1" = runif(10, 0, 10), "v2" = runif(10, 0 ,10), 
               "v3" = runif(10, 0 ,10), "v4" = rep("some_charchter", 10))