How to consolidate rows

Hi,

I am once more seeking help. I want to consolidate rows that have the same material, date in a data set

tibble::tribble(
     ~Item,        ~Date,   ~Description, ~Quantity,
     "Saw", "01/02/2020",      "outdoor",        2L,
     "Saw", "01/02/2020",      "outdoor",        3L,
     "Saw", "01/04/2020",      "outdoor",        4L,
   "nails", "01/02/2020", "construction",        2L,
   "nails", "01/04/2020", "construction",        3L,
   "nails", "01/06/2020", "construction",        4L,
  "hammer", "01/01/2020",         "home",        1L,
  "hammer", "01/01/2020",         "home",        2L,
  "hammer", "01/01/2020",         "home",        3L
  )

Desired Result:

tibble::tribble(
     ~Item,        ~Date,   ~Description, ~Quantity,
     "Saw", "01/02/2020",      "outdoor",        5L,
     "Saw", "01/04/2020",      "outdoor",        4L,
   "nails", "01/02/2020", "construction",        2L,
   "nails", "01/04/2020", "construction",        3L,
   "nails", "01/06/2020", "construction",        4L,
  "hammer", "01/01/2020",         "home",        6L
  )

Thank you very much in advance!

summariseddf <- df %>%
  group_by(date, Description, item) %>%
  summarise(count = sum(Quantity)
library(data.table)

# ernijs12's dataset
data <- as.data.table(    # Structure as data.table
  tibble::tribble(
     ~Item,        ~Date,   ~Description, ~Quantity,
     "Saw", "01/02/2020",      "outdoor",        2L,
     "Saw", "01/02/2020",      "outdoor",        3L,
     "Saw", "01/04/2020",      "outdoor",        4L,
   "nails", "01/02/2020", "construction",        2L,
   "nails", "01/04/2020", "construction",        3L,
   "nails", "01/06/2020", "construction",        4L,
  "hammer", "01/01/2020",         "home",        1L,
  "hammer", "01/01/2020",         "home",        2L,
  "hammer", "01/01/2020",         "home",        3L
  )
)

# Consolidate; format [Date] from 'chr' to 'Date'
data_a <- data[, list(Quantity = sum(Quantity)), list(Item, Date = as.Date(Date, "%m/%d/%y"), Description)]
data_a # Print
     Item       Date  Description Quantity
1:    Saw 2020-01-02      outdoor        5
2:    Saw 2020-01-04      outdoor        4
3:  nails 2020-01-02 construction        2
4:  nails 2020-01-04 construction        3
5:  nails 2020-01-06 construction        4
6: hammer 2020-01-01         home        6

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.