R 尝试在不删除其他列的情况下在数据帧中折叠多个观测值
我有很多重复的数据框,我尝试了不同的方法将它们折叠在一起,但它总是删除price和total列R 尝试在不删除其他列的情况下在数据帧中折叠多个观测值,r,dataframe,dplyr,tidyverse,R,Dataframe,Dplyr,Tidyverse,我有很多重复的数据框,我尝试了不同的方法将它们折叠在一起,但它总是删除price和total列 > Total Product Quantity Price Total 1: tomatoes 1kg 1 16 16 2: small cucumber 1 10 10 3: beetr
> Total
Product Quantity Price Total
1: tomatoes 1kg 1 16 16
2: small cucumber 1 10 10
3: beetroot 1kg 1 15 15
4: potatoes 1 kg 1 15 15
5: asparagus 200g 4 45 180
6: red apples 4 medium 1 10 10
7: beef fillet strips 500g 1 90 90
8: back bacon 200g 1 30 30
9: chicken drums and thighs 1 kg 1 75 75
10: kudu biltong 250g 2 80 160
11: t bone 500g 1 66 66
12: free range eggs 6 1 15 15
13: tomatoes 1kg 1 16 16
14: calistos jalape=c3=b1o salsa 1 40 40
15: lean beef mince 1 54 54
16: free range eggs 30 1 65 65
17: potatoes 1 kg 1 15 15
18: strawberry punnet 250g 1 22 22
19: chicken whole 1.4 kg 1 65 65
20: small cucumber 4 10 40
21: swiss chard 2 14 28
22: tomatoes 1kg 3 16 48
23: carrot 1 kg 2 14 28
24: kale 2 14 28
25: butternut cubes 2 14 28
26: potatoes 1 kg 2 15 30
27: onions 1 kg 1 15 15
28: oyster mushrooms 200g punnet 1 35 35
29: strawberry punnet 250g 2 22 44
30: free range eggs 30 1 65 65
31: small cucumber 2 10 20
32: tomatoes 1kg 1 16 16
33: broccoli head 1 25 25
34: cauliflower whole head 2 25 50
35: carrot 1 kg 2 14 28
36: butternut cubes 2 14 28
37: kale 2 14 28
38: butter 500g 1 57 57
39: oyster mushrooms 200g punnet 2 35 70
40: coleslaw 1 15 15
Product Quantity Price Total
让我明白:
aggregate(Quantity~Product,data=Total,FUN=sum)
这会折叠它,但会删除其他列
> Total
Product Quantity
1 asparagus 200g 4
2 back bacon 200g 1
3 beef fillet strips 500g 1
4 beetroot 1kg 1
5 broccoli head 1
6 butter 500g 1
7 butternut cubes 4
8 calistos jalape=c3=b1o salsa 1
9 carrot 1 kg 4
10 cauliflower whole head 2
11 chicken drums and thighs 1 kg 1
12 chicken whole 1.4 kg 1
13 coleslaw 1
14 free range eggs 30 2
15 free range eggs 6 1
16 kale 4
17 kudu biltong 250g 2
18 lean beef mince 1
19 onions 1 kg 1
20 oyster mushrooms 200g punnet 3
21 potatoes 1 kg 4
22 red apples 4 medium 1
23 small cucumber 7
24 strawberry punnet 250g 3
25 swiss chard 2
26 t bone 500g 1
27 tomatoes 1kg 6
做同样的事情
预期输出应具有价格
和总计
,并合并所有产品
试试这个:
Total %>% group_by(Product) %>% summarise(quantity = sum(Quantity))
summary
将只保留分组列和使用聚合函数计算的列
如果给定的产品
始终具有相同的价格
,您可以执行以下操作:
df = data.frame(product = c("tomatoes 1kg", "tomatoes 1kg", "small cucumber"),
quantity = c(1, 1, 1),
price = c(16, 16, 10),
total = c(16, 16, 10))
df[-duplicated(df),]
[顺便说一句,我个人不建议将数据帧命名为“Total”,如果它已经是列的名称,那么这有点让人困惑。]
如果给定的产品
并不总是具有相同的价格
(例如,如果数据来自多个日期,且价格可能会随时间而变化),那么您必须决定(a)您是否仍然希望每个产品
,或者(B)您希望每个不同的(产品
,价格
)都有一行一对如果选择(A),则必须为每个产品
选择要包含的价格
。为该产品列出的第一个价格
?最低价格
?最高价格
?平均价格是多少?等等
这就解释了为什么summary
会删除groupby
或aggregate函数中未包含的列:除非您告诉它,否则它知道您将选择上述哪些选项
如果您想保持每种产品的平均价格,您可以这样做:
Total %>% group_by(Product, Price) %>% summarise(Quantity = sum(Quantity), Total = sum(Total))
[在这里,我不只是做AveragePrice=mean(Price)
,因为这将是该产品每行的平均价格,而您可能想要的是每单位销售的平均价格。]请熟悉如何提供适当且合理的价格
下面是一个dplyr
解决方案
df=data.frame(产品=c(“西红柿1kg”、“西红柿1kg”、“小黄瓜”),
数量=c(1,1,1),
价格=c(16,16,10),
总计=c(16,16,10))
图书馆(dplyr)
df%>%
分组(产品、价格)%>%
总结(
数量=总和(数量),
总计=总和(总计)
)
#>#A tibble:2 x 4
#>#群体:产品[2]
#>产品价格数量合计
#>
#>1个小黄瓜10110
#>2个西红柿1公斤16 2 32
由(v0.3.0)于2020年11月12日创建。使用聚集将列中的变量表示为行。然后使用group和summary函数获得每组的总和/平均值。同样,可以使用排列按列显示数据
Total %>% group_by(Product) %>%
summarise(Quantity = sum(Quantity),
AveragePrice = sum(Total)/sum(Quantity),
Total = sum(Total))
您是否尝试过聚合(.~Product,data=Total,FUN=sum)
>最终数量188
给我这个,你的预期输出是什么?这不起作用,我目前大部分代码都是以文本形式运行的,所以它以原始文本的形式出现,我将其转换为数据帧,所以我必须手动对100多个产品执行此操作,我希望避免DF只是一个示例数据集。在提问时提供数据集是一种很好的做法。我为你创造了一个。请检查答案在您的大型数据集上是否有效错误:duplicated()仅适用于向量
我应该补充,我假设在这个答案中,您所谓的“duplicates”代表不同的观察结果(即,同一产品的不同购买)。但是,如果它们实际上是相同观察的重复记录(例如意外记录了两次的购买),您可以这样消除它们:Total%>%distinct()
数量总计1611685
第一个给我这个数量平均价格总计16127.622951685
第二个给我这个。是的,对不起,我是说单独的观察,因为这些是使用rbind
和map
连接的数据帧,我找不到使用left\u-join
或full\u join
与map`quantity total`1 61 1685
一样,我想这可能是我获取此数据框的方式,我不确定,但我需要所有列出价格的产品。请提供一个可复制的示例和适当的预期输出。根据你的问题,我们很难知道你在寻找什么。很难提供一个可复制的例子,因为我不得不多次进行文本筛选和转换变量来创建数据框。我的预期输出与你的相同,但我只是得到所有列的完整总和,而不是你的结果。这是一个糟糕的说法我的意思是我得到了数量
和总数
列的总和,但没有显示任何产品。我需要使用一个可复制的示例来进一步帮助您。要生成最小数据集,可以使用head()
、subset()
或索引。然后使用dput()
给我们一些可以立即放入R的东西。
Total %>%
gather(key = variable, value = value, c(Quantity,Price,Total)) %>%
group_by(Product, variable) %>%
summarize(sum = sum(value)) %>%
spread(variable, sum)