R 在没有任何现有模式逻辑的情况下聚合两个数据帧列

R 在没有任何现有模式逻辑的情况下聚合两个数据帧列,r,dataframe,R,Dataframe,我需要通过准合并两列来聚合数据帧的值 一方面,需要更改某些行值(更改名称),另一方面,需要进行没有任何模式或逻辑的手动聚合。由于这听起来可能很复杂或难以理解,请检查示例代码和图像 数据集如下所示: set.seed(1253) dates <- as.Date(c(Sys.Date()+10)) fruits <- c("Apple","Apple","Apple","Apple","Banana","Banana","Banana","Banana", "Strawberry"

我需要通过准合并两列来聚合数据帧的值

一方面,需要更改某些行值(更改名称),另一方面,需要进行没有任何模式或逻辑的手动聚合。由于这听起来可能很复杂或难以理解,请检查示例代码和图像

数据集如下所示:

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)

theDF
set.seed(1253)
日期
set.seed(1253)

日期请使用
set.seed
使此可复制,因为
样本
是used@Frank-给我几分钟时间编辑整篇文章,因为我在绿色和蓝色葡萄上又犯了一个错误。@akrun希望现在可以找到一个solution@Hack-R是的,刚刚添加了正确的解决方案有人已经添加了解决方案。所以,希望能有所帮助。非常感谢,这终于解决了数据集的问题!:)@MHN很乐意帮忙
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)

theDF
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 stock
1 2016-06-11       Apple    40
2 2016-06-11      Banana    37
3 2016-06-11   Big Grape    15
4 2016-06-11        Kiwi    33
5 2016-06-11 Small Grape    21
6 2016-06-11  Strawberry    64
>