将行重新整形为列,具有两个条件(a~b,每c,每d)

时间:2013-01-03 18:09:07

标签: r reshape

我有一个像这样的数据框:

item   price source   day
book   5     shop     2012-01-01
car    100   shop     2012-01-01
desk   10    shop     2012-01-01
book   4     internet 2012-01-01
car    99    internet 2012-01-01
desk   9     internet 2012-01-01
book   6     shop     2012-01-02
car    101   shop     2012-01-02
desk   11    shop     2012-01-02
book   4     internet 2012-01-02
car    100   internet 2012-01-02
desk   10    internet 2012-01-02

我希望每天每件商品的价格如下:

item price.shop price.internet day
book 5          4              2012-01-01
car  100        99             2012-01-01
desk 10         9              2012-01-01
book 6          5              2012-01-02
car  101        100            2012-01-02
desk 11         10             2012-01-02

我一直在努力

melt'然后

cast(df, item ~ source + variable)

但无法获得理想的结果。我认为问题是因为我本质上有两个“每件事”条件(每day,每source),因此没有正确的等式。

感谢您的帮助

3 个答案:

答案 0 :(得分:6)

不是plyr.reshape2的答案,而是使用base:reshape():

> reshape(dat, idvar=c("item", "day"),  timevar="source", direction="wide")
  item        day price.shop price.internet
1 book 2012-01-01          5              4
2  car 2012-01-01        100             99
3 desk 2012-01-01         10              9
7 book 2012-01-02          6              4
8  car 2012-01-02        101            100
9 desk 2012-01-02         11             10

我想也可以尝试一种merge(subset_shop, subset_internet, 1:2, all=TRUE)策略。

答案 1 :(得分:4)

这样的事情可能是:

dat <- read.table(text = "item   price source   day
book   5     shop     2012-01-01
car    100   shop     2012-01-01
desk   10    shop     2012-01-01
book   4     internet 2012-01-01
car    99    internet 2012-01-01
desk   9     internet 2012-01-01
book   6     shop     2012-01-02
car    101   shop     2012-01-02
desk   11    shop     2012-01-02
book   4     internet 2012-01-02
car    100   internet 2012-01-02
desk   10    internet 2012-01-02",header = TRUE,sep = "")

> library(reshape2)
> dcast(dat,item + day ~ source,fun.aggregate = sum,value.var = "price")

  item        day internet shop
1 book 2012-01-01        4    5
2 book 2012-01-02        4    6
3  car 2012-01-01       99  100
4  car 2012-01-02      100  101
5 desk 2012-01-01        9   10
6 desk 2012-01-02       10   11

答案 2 :(得分:2)

一种可能意想不到的方法(因为Joran和DWin立即敲响了重塑的替代方案):ftablextabs一起工作。假设data.frame称为“dat”:

ftable(xtabs(price ~ item + day + source, dat))
#                 source internet shop
# item day                            
# book 2012-01-01               4    5
#      2012-01-02               4    6
# car  2012-01-01              99  100
#      2012-01-02             100  101
# desk 2012-01-01               9   10
#      2012-01-02              10   11