-1

The question is: How many households live in area 2, earn more than 20k, and have purchased more than 30 units of pasta over the whole time period?

I tried

pasta  %>%
  filter(AREA=="2") %>% group_by(HHID) %>% 
  summarise(buy2= sum(PASTA), INCOME) %>% 
  summarise(income2 = INCOME > "20000" && buy2 > "30")

# run and show 
  `summarise()` has grouped output by 'HHID'. You can override using the `.groups` argument.
  A tibble: 403 x 2
    HHID income2
   <int> <lgl>  
 1    10 TRUE   
 2    16 TRUE   
 3    17 FALSE  
 4    29 TRUE   
 5    30 FALSE  
 6    31 FALSE  
 7    36 TRUE   
 8    42 FALSE  
 9    47 TRUE   
10    60 TRUE   
# … with 393 more rows

How can I count the 'TRUE'?

Martin Gal
  • 16,640
  • 5
  • 21
  • 39
redgoose
  • 1
  • 2
  • The argument is `.groups` and it should be `.groups = 'drop'` instead of `.group(HHID)` – akrun Sep 09 '21 at 20:09
  • Hard to help you if you dont share the data – GuedesBF Sep 09 '21 at 20:11
  • 2
    redgoose, welcome to SO! Please see https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info for great discussions on how to make a question *reproducible*, specifically providing usable, unambiguous sample data (`dput(.)`). – r2evans Sep 09 '21 at 20:22
  • I can share the dataset, please let me know where to upload. – redgoose Sep 09 '21 at 20:39

1 Answers1

1

If we need to count the logical, use sum. In addition, replace the && with & and remove the quotes around the numeric values

...
 summarise(Count = sum(INCOME > 20000 & buy2 > 30, na.rm = TRUE), 
        .groups = 'drop')
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Can you show your `packageVersion('dplyr')` – akrun Sep 09 '21 at 20:21
  • install.packages("tidyverse") install.packages("ggplot2") library(tidyverse) library(ggplot2) – redgoose Sep 09 '21 at 20:24
  • @redgoose I would just load `library(dplyr);library(ggplot2)` on a fresh R session and do this again. Loading tidyverse implies a lot of packages and that may be not needed in this code. In addition, some packages have functions that can mask the dplyr functions – akrun Sep 09 '21 at 20:25