How to ignore rows with 0

Example data set:

    tibble::tribble(
         ~Item,        ~Date, ~quantity,
         "saw", "01/01/2020",        0L,
         "saw", "02/01/2020",       10L,
         "saw", "03/01/2020",        5L,
         "saw", "04/01/2020",       20L,
         "saw", "05/01/2020",       30L,
         "saw", "06/01/2020",        0L,
         "saw", "07/01/2020",        0L,
      "hammer", "08/01/2020",       60L,
      "hammer", "09/01/2020",       70L,
      "hammer", "10/01/2020",       40L
      )

I want to subtract quantity in the min date from the max date for each material and so it ignores the dates that have 0

desired result:

    tibble::tribble(
      ~Item, ~difference,
      "saw",        -20L,
      "hammer",       20L
      )

As a university professor, this is what I would tell one of my students to help them get the answer:

Step 1 = filter your tibble for quantity > 0
Step 2 = group the data by Item
Step 3 = summarize the difference between the first and last values of quantity. The result will be one value for each group.

You will use the filter(), group_by(), summarize(), first() and last() functions.

I am confused by what values you want to get. If by min date you mean the first one that has a non-zero quantity, and max date is the last one with a non-zero quantity, then subtracting the min date quantity for the saw group (10) from the max date quantity (30) would be 20, not -20.

1 Like

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.