【发布时间】:2017-10-25 08:46:36
【问题描述】:
我有一个数据集,其中包含不同产品的买卖价格信息。但是,它不是将购买的价格和出售的价格存储在同一行中,而是存储在两个单独的行中,这两个行由买入和卖出的变量标识,如下所示。
Product|Product Type|Price|Bought|Sold
---------------------------------------
Apples | Green | 1 | 0 | 1
---------------------------------------
Apples | Green | 2 | 1 | 0
---------------------------------------
Apples | Red | 3 | 0 | 1
---------------------------------------
Apples | Red | 4 | 1 | 0
---------------------------------------
我想把买入和卖出的价格合并成一行,所以看起来有点像这样:
Product|Product Type|Bought Price|Sold Price
---------------------------------------------
Apples | Green | 1 | 2
---------------------------------------------
Apples | Red | 4 | 3
这是创建我的示例数据集的代码。提前感谢您的帮助。
Product <- c("Apples", "Apples", "Apples", "Apples", "Apples", "Apples",
"Oranges", "Oranges", "Oranges", "Oranges", "Oranges", "Oranges",
"Buscuits", "Buscuits", "Buscuits", "Buscuits", "Buscuits", "Buscuits")
ProductType <- c("Green", "Green", "Red", "Red", "Pink", "Pink",
"Big", "Big", "Medium", "Medium", "Small", "Small",
"Chocolate", "Chocolate", "Oat", "Oat", "Digestive", "Digestive")
Price <- c(2, 1, 3, 4, 1, 2,
5, 3, 2, 1, 2, 3,
6, 4, 1, 8, 6, 2)
Bought <- c(0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1,
0, 1, 0, 1, 0, 1)
Sold <- c(1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0,
1, 0, 1, 0, 1, 0)
sales <- data.frame(Product, ProductType, Price, Bought, Sold)
【问题讨论】:
-
买/卖中的 1 是coolean yes/no 还是数量指示?
-
试试
sales %>% group_by(Product, ProductType) %>% summarise(BoughtPrice = Price[Bought==1], SoldPrice = Price[Sold ==1])