R 两列命名水果的自定义聚合
我想通过以下某种特殊的方式,按名称聚合数据帧的两列:R 两列命名水果的自定义聚合,r,dataframe,aggregate,R,Dataframe,Aggregate,我想通过以下某种特殊的方式,按名称聚合数据帧的两列: 通过特别聚合两列结果和部分 苹果、香蕉和草莓的部分值无关紧要,所有东西都得到了总结,而葡萄和猕猴桃的部分值应该成为新的水果名称 结果(在底部)应该有8个聚合行,而不是20个 乍一看,这听起来可能非常简单,但经过数小时的反复试验,我没有找到任何有用的解决方案。下面是一个例子: theDF <- data.frame(dates = as.Date(c(today()+20)), fruits = c("Apple","Appl
- 通过特别聚合两列
和结果
部分
- 苹果、香蕉和草莓的
值无关紧要,所有东西都得到了总结,而葡萄和猕猴桃的部分
值应该成为新的部分
名称水果
- 结果(在底部)应该有8个聚合行,而不是20个
theDF <- data.frame(dates = as.Date(c(today()+20)),
fruits = c("Apple","Apple","Apple","Apple","Banana","Banana","Banana","Banana",
"Strawberry","Strawberry","Strawberry","Strawberry","Grape", "Grape",
"Grape","Grape", "Kiwi","Kiwi","Kiwi","Kiwi"),
parts = c("Big Green Apple","Apple2","Blue Apple","XYZ Apple4",
"Yellow Banana1","Small Banana","Banana3","Banana4",
"Red Small Strawberry","Red StrawberryY","Big Strawberry",
"StrawberryZ","Green Grape", "Blue Grape", "Blue Grape",
"Blue Grape","Big Kiwi","Small Kiwi","Big Kiwi","Middle Kiwi"),
stock = as.vector(sample(1:20)) )
theDF我们可以使用data.table
。如果要删除的“部分”列中有诸如结尾字符是大写字母或数字之类的模式,我们可以使用sub
来执行此操作,并将其与“日期”一起用作分组变量,并获取“股票”的和
library(data.table)
setDT(theDF)[,.(stock = sum(stock)) , .(dates, fruits = sub("([0-9]|[A-Z])$", "", parts))]
# dates fruits stock
#1: 2016-06-19 Apple 46
#2: 2016-06-19 Banana 35
#3: 2016-06-19 Strawberry 38
#4: 2016-06-19 Green Grape 12
#5: 2016-06-19 Blue Grape 21
#6: 2016-06-19 Big Kiwi 37
#7: 2016-06-19 Small Kiwi 14
#8: 2016-06-19 Middle Kiwi 7
或者使用dplyr
,我们同样可以实现相同的方法
library(dplyr)
theDF %>%
group_by(dates, fruits = sub('([0-9]|[A-Z])$', '', parts)) %>%
summarise(stock = sum(stock))
更新
如果没有模式且仅基于手动识别“水果”中的元素,则创建元素的向量
,使用%chin%
获取“i”中的逻辑索引,将(:=
)与“水果”对应的“部分”中的值分配给“水果”,然后按“日期”进行分组,“水果”并获取“股票”的总和
setDT(theDF)[as.character(fruits) %chin% c("Grape", "Kiwi"),
fruits := parts][, .(stock = sum(stock)), .(dates, fruits)]
数据
theDF另一种方法是在第一步中创建一个适当的分组变量,然后使用您喜欢的按组汇总的方法。在这里,我使用dplyr
,您可以使用其他(data.table
,等等)
库(dplyr)
THDF%汇总(存量=总额(存量))
来源:本地数据帧[8 x 2]
水果库存
(chr)(内部)
1苹果34
2香蕉35
3大猕猴桃26
4蓝葡萄32
5青葡萄7
6中猕猴桃12
7小猕猴桃19
8草莓45
我没有找到函数today()
,因此跳过了日期列。您可以通过在分组中插入date
将其添加回去,如groupby(fruits,date)
将其保留在所需的输出中。感谢您在@akrun进行的精彩快速回放。但我想我的考试有误导性。在我的真实数据中,没有数字或大写字母。一切都是混合的。还有“大草莓”。因此,我需要一个通过手动选择零件名称来工作的解决方案。@MHN您提到过手动选择零件
名称。我们还可以依赖其他模式吗?例如,如何选择部分
或水果
列中的条目?我已经编辑了原始帖子。很抱歉给您带来不便。事实上,没有任何逻辑模式。我认为这是一个很好的描述我的问题-aggreating专栏没有任何模式。绝对了不起!这正是我想要的。真的很棒!非常感谢。嗨,不幸的是,我需要在上面做一个新的编辑,因为出现了一个新问题。也许你有办法解决这个问题?谢谢:)我想你需要require(lubridate)
来完成today()
功能
theDF <- structure(list(dates = structure(c(16971, 16971, 16971, 16971,
16971, 16971, 16971, 16971, 16971, 16971, 16971, 16971, 16971,
16971, 16971, 16971, 16971, 16971, 16971, 16971), class = "Date"),
fruits = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 5L,
5L, 5L, 5L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("Apple",
"Banana", "Grape", "Kiwi", "Strawberry"), class = "factor"),
parts = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 14L,
15L, 16L, 16L, 11L, 10L, 10L, 10L, 9L, 13L, 9L, 12L), .Label = c("Apple1",
"Apple2", "Apple3", "Apple4", "Banana1", "Banana2", "Banana3",
"Banana4", "Big Kiwi", "Blue Grape", "Green Grape", "Middle Kiwi",
"Small Kiwi", "StrawberryX", "StrawberryY", "StrawberryZ"
), class = "factor"), stock = c(8, 19, 15, 4, 6, 18, 1, 10,
9, 16, 11, 2, 12, 13, 5, 3, 17, 14, 20, 7)), .Names = c("dates",
"fruits", "parts", "stock"), row.names = c(NA, -20L), class = "data.frame")
library(dplyr)
theDF <- data.frame(fruits, parts, stock, stringsAsFactors = F)
theDF$fruits <- with(theDF, ifelse(fruits=="Kiwi" | fruits=="Grape", parts, fruits))
theDF %>% group_by(fruits) %>% summarise(stock = sum(stock))
Source: local data frame [8 x 2]
fruits stock
(chr) (int)
1 Apple 34
2 Banana 35
3 Big Kiwi 26
4 Blue Grape 32
5 Green Grape 7
6 Middle Kiwi 12
7 Small Kiwi 19
8 Strawberry 45