0
country     continent  year lifeExp      
   <fct>       <fct>     <int>   <dbl>    
 1 Afghanistan Asia       1952    28.8        
 2 Afghanistan Asia       1957    30.3     
 3 Afghanistan Asia       1962    32.0    
 4 Afghanistan Asia       1967    34.0      
 5 Afghanistan Asia       1972    36.1     
 6 Afghanistan Asia       1977    38.4    
 7 Afghanistan Asia       1982    39.9      
 8 Afghanistan Asia       1987    40.8       
 9 Afghanistan Asia       1992    41.7       
10 Afghanistan Asia       1997    41.8

I want to print the observations for Afghanistan where the corresponding values for lifeExp are 41, using the packages dplyr and tidyverse.

I have tried subsetting using $,pull, filter and select.

Sotos
  • 51,121
  • 6
  • 32
  • 66
Galadriel
  • 17
  • 4
  • 3
    How did you do the filter? It's quite straight forward. Did you try `df %>% filter(country == 'Afghanistan' , lifeExp >= 41)` or `... == 41`...whatever – Sotos Oct 25 '19 at 14:07
  • can you share your data using `dput(your_data_frame)` please ? – cbo Oct 25 '19 at 14:10
  • This is a task that `dplyr` is really useful for. I would suggest looking at the resources available on the [dplyr website](https://dplyr.tidyverse.org/). – bouncyball Oct 25 '19 at 14:14
  • You will always get better answers if you include your data. See [this](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for help on how to make your question reproducible. In your case it's easy, because the data is already in a package (`gapminder`), but not everyone that is willing to help you will know that. – natej Oct 25 '19 at 14:18

1 Answers1

0

You were on the right track with filter(). The reason you wouldn't get any observations printed filtering for lifeExp == 41 is that there are no observations that are exactly equal to 41:

library(gapminder)
library(dplyr)

data(gapminder)
filter(gapminder, country == "Afghanistan" & lifeExp == 41)
#> # A tibble: 0 x 6
#> # ... with 6 variables: country <fct>, continent <fct>, year <int>,
#> #   lifeExp <dbl>, pop <int>, gdpPercap <dbl>

You either need to specify a range, or round the values before filtering:

filter(gapminder, country == "Afghanistan" & lifeExp > 39 & lifeExp < 42)
#> # A tibble: 4 x 6
#>   country     continent  year lifeExp      pop gdpPercap
#>   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
#> 1 Afghanistan Asia       1982    39.9 12881816      978.
#> 2 Afghanistan Asia       1987    40.8 13867957      852.
#> 3 Afghanistan Asia       1992    41.7 16317921      649.
#> 4 Afghanistan Asia       1997    41.8 22227415      635.

gapminder %>%
  mutate(lifeExp = round(lifeExp)) %>%
  filter(country == "Afghanistan" & lifeExp == 41)
#> # A tibble: 1 x 6
#>   country     continent  year lifeExp      pop gdpPercap
#>   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
#> 1 Afghanistan Asia       1987      41 13867957      852.
natej
  • 128
  • 7