本文介绍了聚合两个数据帧列,而不存在任何现有的模式逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我需要通过准合并两列汇总数据框的值。 一方面,需要更改一些行值(更改名称),另一方面需要进行没有任何模式或逻辑的手动聚合。由于这可能听起来很复杂或难以理解,请查看示例代码和图像。 数据集如下所示: set.seed(1253) dates< - as.Date(c(Sys.Date()+ 10)) fruits< - c (苹果苹果,苹果,苹果,香蕉,香蕉,香蕉,香蕉,草莓,草莓,草莓 葡萄,葡萄,葡萄,葡萄,猕猴桃,猕猴桃,猕猴桃,猕猴桃) parts< - c 绿色苹果,默认,蓝苹果,XYZ Apple4,黄色香蕉1,小香蕉,香蕉3,香蕉4,红色小草莓 ,红草莓,大草莓,草莓Z,绿葡萄,绿葡萄,蓝葡萄,蓝葡萄,大猕猴桃猕猴桃,猕猴桃,默认)股票< - as.vector(样本(1:20)) theDF< - data.frame零件,库存) theDF 中间步骤来纠正聚合: 希望,有一个解决方案。感谢提前!解决方案 set.seed(1253) dates< ; - as.Date(c(Sys.Date()+ 10))水果< - c(苹果,苹果,苹果,苹果,香蕉 ,香蕉,香蕉,草莓,草莓,草莓,草莓,葡萄,葡萄,葡萄,葡萄猕猴桃,猕猴桃,猕猴桃,猕猴桃) parts 黄香蕉1,小香蕉,香蕉3,香蕉4,红色小草莓,红色草莓Y,大草莓,草莓Z,绿色葡萄 绿葡萄,蓝葡萄,蓝葡萄,大猕猴桃,小猕猴桃,猕猴桃,默认)股票< - as.vector (1:20)) theDF< - data.frame(日期,水果,零件,股票) theDF 有几种方法可以做到这一点,如果你有更多的零件值,我建议使用som e自定义正则表达式来帮助。只有这样一个可管理的数字,这样做更容易如下。 theDF $ fruits< - as.character(theDF $ fruits) theDF $ fruits [theDF $ fruits ==Grape& theDF $ parts ==蓝葡萄]< - 小葡萄 theDF $ fruits [theDF $ fruits ==Grape& theDF $ parts ==Green Grape]< - Big Grape df< - 聚合(theDF $ stock,by = list(theDF $ dates,theDF $ fruits),FUN =总和) colnames(df)< - c(日期,水果,股票) df 日期水果股票 1 2016- 06-11苹果40 2 2016-06-11香蕉37 3 2016-06-11大葡萄15 4 2016-06-11猕猴桃33 5 2016-06 -11小葡萄21 6 2016-06-11草莓64 > I need to aggregate the values of a data frame by quasi merging two columns. On one hand some row values (changing the names) needs to be changed, on the other hand a manual aggregation without any pattern or logic needs to be made. Since this may sound complex or unintelligibly, please check the example code and images.The data set looks like this:set.seed(1253)dates <- as.Date(c(Sys.Date()+10))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","Default","Blue Apple","XYZ Apple4", "Yellow Banana1","Small Banana","Banana3","Banana4", "Red Small Strawberry","Red StrawberryY","Big Strawberry", "StrawberryZ", "Green Grape", "Green Grape", "Blue Grape", "Blue Grape", "Big Kiwi","Small Kiwi", "Kiwi","Default")stock <- as.vector(sample(1:20))theDF <- data.frame(dates, fruits, parts, stock)theDFThe intermediate step to correct aggregation:The final data frame should look like this:Hopefully, there is a solution. Thanks in advance! 解决方案 set.seed(1253)dates <- as.Date(c(Sys.Date()+10))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","Default","Blue Apple","XYZ Apple4", "Yellow Banana1","Small Banana","Banana3","Banana4", "Red Small Strawberry","Red StrawberryY","Big Strawberry", "StrawberryZ", "Green Grape", "Green Grape", "Blue Grape", "Blue Grape", "Big Kiwi","Small Kiwi", "Kiwi","Default")stock <- as.vector(sample(1:20))theDF <- data.frame(dates, fruits, parts, stock)theDFThere are several ways to do this, if you have a lot more values of "parts" I'd recommend using some custom regex to help out. With only a manageable number like this it's easier to do it as follows.theDF$fruits <- as.character(theDF$fruits)theDF$fruits[theDF$fruits == "Grape" & theDF$parts == "Blue Grape"] <- "Small Grape"theDF$fruits[theDF$fruits == "Grape" & theDF$parts == "Green Grape"] <- "Big Grape"df <- aggregate(theDF$stock, by = list(theDF$dates, theDF$fruits), FUN = sum)colnames(df) <- c("dates", "fruits", "stock")df dates fruits stock1 2016-06-11 Apple 402 2016-06-11 Banana 373 2016-06-11 Big Grape 154 2016-06-11 Kiwi 335 2016-06-11 Small Grape 216 2016-06-11 Strawberry 64> 这篇关于聚合两个数据帧列,而不存在任何现有的模式逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-24 15:08