R中按组进行的外部操作 [英] Outer operation by group in R

查看:32
本文介绍了R中按组进行的外部操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题涉及计算每个时期每个产品的价格差异.在下面的示例数据中

My problem involves calculating differences in prices across products for each period. With the sample data below

product = c('A','A','A','B','B','B','C','C','C')
date = as.Date(c('2016-09-12','2016-09-19', '2016-09-26','2016-09-12','2016-09-19', '2016-09-26', '2016-09-12','2016-09-19', '2016-09-26'))
price = as.numeric(c(17, 14.7, 15, 14.69, 14.64, 14.63, 13.15, 13.15, 13.15))

df <- data.frame(product, date, price)

挑战在于分组,没有分组,对外部函数的简单调用就可以解决问题.

The challenge is in the grouping, without which a simple call to the outer function could do the trick.

melt(outer(df$price, df$price, "-"))

但是,将其与 dplyr 中的transmute函数结合使用会导致看起来很奇怪的错误消息错误:与STRSXP不兼容".在线评论表明这可能是由于软件包中的错误所致.

However combining this with the transmute function in dplyr leads to a strange-looking error message "Error: not compatible with STRSXP". Comments online suggest this might be due to a bug in the package.

所以我想知道是否有人对替代方法有个很好的建议.

So I am wondering whether anyone has a neat suggestion for an alternative approach.

理想情况下,我还在以下几行中寻找输出内容.

Ideally, I am looking for output something also the following lines.

Var1 Var2 Date          value
A    A    '2016-09-12'  0.00
A    B    '2016-09-12'  2.31
A    C    '2016-09-12'  3.85
B    A    '2016-09-12' -2.31
B    B    '2016-09-12'  0.00
B    C    '2016-09-12'  1.54
C    A    '2016-09-12' -3.85
C    B    '2016-09-12' -1.54
C    C    '2016-09-12'  0.00
A    A    '2016-09-19'  0.00
A    B    '2016-09-19'  0.06
A    C    '2016-09-19'  1.55

等.赞赏的是,这会留下一些多余的对,但这会使生活更轻松.

etc, etc. Appreciate this leaves some redundant pairs, but that makes life easier further down the line.

谢谢您的关注.:)

推荐答案

通常,如果数据转换不适用于 mutate / transform ,则可以尝试要做:

In general, if a data transformation doesn't work with mutate/transform, you can try do:

> library(dplyr)
> df %>% 
   group_by(date) %>% 
   do(reshape2::melt(outer(.$price, .$price, "-")))

Source: local data frame [27 x 4]
Groups: date [3]

         date  Var1  Var2 value
       (date) (int) (int) (dbl)
1  2016-09-12     1     1  0.00
2  2016-09-12     2     1 -2.31
3  2016-09-12     3     1 -3.85
4  2016-09-12     1     2  2.31
5  2016-09-12     2     2  0.00
6  2016-09-12     3     2 -1.54
7  2016-09-12     1     3  3.85
8  2016-09-12     2     3  1.54
9  2016-09-12     3     3  0.00
10 2016-09-19     1     1  0.00
..        ...   ...   ...   ...

这篇关于R中按组进行的外部操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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