Dplyr Mutate_each用于配对列 [英] Dplyr Mutate_each for paired sets of columns

查看:103
本文介绍了Dplyr Mutate_each用于配对列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用dplyr :: mutate_each来实现以下转换?

Is there a way to achieve the following transformation using dplyr::mutate_each?

data.frame(x1 = 1:5, x2 = 6:10, y1 = rnorm(5), y2 = rnorm(5)) %>%
  mutate(diff1 = x1 - y1, diff2 = x2 - y2) 

##   x1 x2          y1         y2       diff1     diff2
## 1  1  6  1.03645018 -0.8602099 -0.03645018  6.860210
## 2  2  7 -1.10790835  1.6912875  3.10790835  5.308712
## 3  3  8  0.95452119  2.7232657  2.04547881  5.276734
## 4  4  9  0.01370762  1.6385765  3.98629238  7.361424
## 5  5 10  0.19354354 -1.0464360  4.80645646 11.046436

我知道这是一个微不足道的例子,很容易完成,如我所描述的,但我正在尝试用更大的列来完成类似的事情。

I realize this is a trivial example and is easily done as I've described, but I'm trying to accomplish a similar thing with a much larger set of columns.

谢谢

推荐答案

根据@Gregor在评论中的提及,如果你想工作使用 dplyr ,最好获取您的数据在一个整洁的格式。这是一个想法:

As per mentionned by @Gregor in the comments, if you want to work with dplyr, it would be better to get your data in a tidy format. Here's an idea:

library(dplyr)
library(tidyr)

df %>%
  add_rownames() %>%
  gather(key, val, -rowname) %>%
  separate(key, c("var", "num"), "(?<=[a-z]) ?(?=[0-9])") %>%
  spread(var, val) %>%
  mutate(diff = x - y) 

其中:

#Source: local data frame [10 x 5]
#
#   rowname   num     x           y        diff
#     (chr) (chr) (dbl)       (dbl)       (dbl)
#1        1     1     1  1.03645018 -0.03645018
#2        1     2     6 -0.86020990  6.86020990
#3        2     1     2 -1.10790835  3.10790835
#4        2     2     7  1.69128750  5.30871250
#5        3     1     3  0.95452119  2.04547881
#6        3     2     8  2.72326570  5.27673430
#7        4     1     4  0.01370762  3.98629238
#8        4     2     9  1.63857650  7.36142350
#9        5     1     5  0.19354354  4.80645646
#10       5     2    10 -1.04643600 11.04643600






如果由于某种原因,您仍然希望在执行操作后使用宽格式的数据,则可以添加到管道中:


If for some reason you still want the data in wide format after performing the operation, you could add to the pipe:

  gather(key, value, -(rowname:num)) %>%
  unite(key_num, key, num, sep = "") %>%
  spread(key_num, value)

哪个会给:

#Source: local data frame [5 x 7]
#
#  rowname       diff1     diff2    x1    x2          y1         y2
#    (chr)       (dbl)     (dbl) (dbl) (dbl)       (dbl)      (dbl)
#1       1 -0.03645018  6.860210     1     6  1.03645018 -0.8602099
#2       2  3.10790835  5.308713     2     7 -1.10790835  1.6912875
#3       3  2.04547881  5.276734     3     8  0.95452119  2.7232657
#4       4  3.98629238  7.361423     4     9  0.01370762  1.6385765
#5       5  4.80645646 11.046436     5    10  0.19354354 -1.0464360






数据 / p>


Data

df <- structure(list(x1 = 1:5, x2 = 6:10, y1 = c(1.03645018, -1.10790835, 
0.95452119, 0.01370762, 0.19354354), y2 = c(-0.8602099, 1.6912875, 
2.7232657, 1.6385765, -1.046436)), .Names = c("x1", "x2", "y1", 
"y2"), class = "data.frame", row.names = c("1", "2", "3", "4", "5"))

这篇关于Dplyr Mutate_each用于配对列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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