0

I am trying to find the number of cars that are in inventory by week. I am having trouble counting the number of rows that meet a logical expression though. Here is what I have tried so far.

NCInv <- WeeklyVariableData$New %>%
  group_by(WeekYear) %>%
  nrow(WeeklyVariableData$New[WeeklyVariableData$New$DateAdded < WeeklyVariableData$New$`Contract Date`])

NCInv <- WeeklyVariableData$New %>%
group by(WeekYear) %>%
nrow(DateAdded < `Contract Date`)

NCInv <- WeeklyVariableData$New%>%
  group_by(WeekYear) %>%
  summarize(NCInv = sum(DateAdded < `Contract Date`))```
  • Can you make your post [reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by providing your data using `dput()` or by using a built-in dataset like `mtcars`? – jrcalabrese Nov 28 '22 at 15:55
  • 1
    (Note that `dplyr` functions like `group_by` and `summarize` need the **whole dataframe passed in**. You almost never use `$` with `dplyr`. Your last attempt is wrong only because of the `$New` which needs to be deleted.) – Gregor Thomas Nov 28 '22 at 16:06
  • structure(list(`Contract Date` = structure(c(18262, 18262, 18262, 18262, 18262, 18262, 18262, 18262, 18262, 18262, 18262, 18262, 18262, 18262), class = "Date"), DateAdded = structure(c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), class = "Date"), WeekYear = c("01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019", "01/2019")), row.names = c(NA, 14L), class = "data.frame") – talkingtoducks Nov 28 '22 at 16:15
  • @GregorThomas I see what you are saying. New actually is the name of the data frame in this case. WeeklyVariableData is a list of sheets that came from an Excel file. – talkingtoducks Nov 28 '22 at 16:22
  • Ah, well then your last code looks right if your data is right. From your comment with some sample data, it looks like your `DateAdded` column is mostly (all?) `NA`. So maybe you need the `na.rm` arg in your `sum()`: `summarize(n_date_before_contract = sum(DateAdded < \`Contract Date\`, na.rm = TRUE))` – Gregor Thomas Nov 28 '22 at 16:36
  • Also, typo in my earlier comment, I'm deleting it and reposting corrected (and with `na.rm = TRUE` added): You need sum. When you do math on logical values, TRUE is counted as 1 and FALSE is counted as 0. So `sum(a > b)` is the number of TRUEs, that is the number of times a is greater than b. `mean(a > b)` is the proportion. So generally you can do `WeeklyVariableData %>% group_by(WeekYear) %>% summarize(n_date_before_contract = sum(DateAdded < \`Contract Date\`, na.rm = TRUE))` – Gregor Thomas Nov 28 '22 at 16:37

1 Answers1

0
NCInv <- WeeklyVariableData$New %>%
  group_by(WeekYear) %>%
  count(DateAdded < `Contract Date`) %>%
  rename(NCInv = n) %>%
  select(WeekYear, NCInv)
NCInv