-1

Question: How many people in the dataset are age 25 or younger and are passengers? Use PERSON_TYPE and AGE to answer this question.

Code. people is the name I set for the dataset:

library(tidyverse)
    people %>%
  filter(
    AGE <= 25 &
    PERSON_TYPE == "PASSENGERS") %>%
  count()

I am getting 0. Based on notes from the lecture and a similar problem, I don't understand why I am getting 0. There is another problem similar to this where I am also getting 0 when I try finding how many.

  • 7
    Please, provide a minimal reproducible example: [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – PaulS Jun 26 '22 at 14:28
  • You could possibly use `nrow()` instead of `count()` – Chamkrai Jun 26 '22 at 15:17
  • I get the same with nrow() – Jessica Amey Jun 26 '22 at 15:34
  • 1
    Then, please post a minimal reproducible example with `dput()` so we can help you better – Chamkrai Jun 26 '22 at 15:41
  • At a guess, it should be `PERSON_TYPE == "PASSENGER"` instead of `"PASSENGERS”` but this can _only_ be a guess since you haven't shown us your data. There is nothing wrong with the code as such, but the result depends on your data as well as the code, and we can't see your data. – Allan Cameron Jun 26 '22 at 16:07

1 Answers1

0

next time, please provide a reproducible example. As @Tom Hoel stated, using nrow() instead of count() should return the desired result. If you still want to use count(), you can put the conditions in this function as well:

people %>%
  count(AGE <= 25 & PERSON_TYPE == "PASSENGERS") 
# returns a data.frame with TRUEs and FALSEs
fbeese
  • 118
  • 8