STAT 1080 - Fall 2026
  • Course Info
    • Overview
    • Syllabus
    • Support
    • Schedule
    • Useful links
  • Weekly Materials
    • W1. Introduction
    • W2. RStudio
    • W3. Correlation
    • W4. Programming
    • W5. Tidyverse
    • W6. Dates/Times and Distributions
    • W7. Data Visualization
    • W8. Statistics & Probability
    • W9. Data & Probability
    • W10. Random Variables
    • W11. Parameters & Estimates
    • W12. Statistical Inference
    • W13. Regression
    • W14. Regression
    • W15. Project Meetings

On this page

  • Filter
  • Select
  • Mutate
  • Group_by + Summarize!
    • Bug Pokemon by Generation
    • Pokemon Weight By Type
    • Pokemon HP by Generation
  • Report an issue

Pokemon dplyr practice

Filter

Using poke_types, get Pokemon matching the following criteria:

  1. Pokedex number between 800 and 900
  2. Grass type
  3. Attack stats of more than 120
poke_types |>
  filter(pokedex_no >= 800, pokedex_no <= 900) |>
  filter(type1 == "Grass") |>
  filter(attack > 120)
# A tibble: 1 × 17
    gen pokedex_no img_link name  variant type1 type2 total    hp attack defense
  <dbl>      <dbl> <chr>    <chr> <chr>   <chr> <chr> <dbl> <dbl>  <dbl>   <dbl>
1     8        812 https:/… Rill… <NA>    Grass <NA>    530   100    125      90
# ℹ 6 more variables: sp_attack <dbl>, sp_defense <dbl>, speed <dbl>,
#   species <chr>, height_m <dbl>, weight_kg <dbl>

Select

Using poke_types, get only columns matching any of the following criteria:

  1. columns starting with t
  2. the 9th column
  3. the pokemon name
poke_types |>
  select(starts_with("t"), 9, name)
# A tibble: 1,526 × 5
   type1 type2  total    hp name      
   <chr> <chr>  <dbl> <dbl> <chr>     
 1 Grass Poison   318    45 Bulbasaur 
 2 Grass Poison   405    60 Ivysaur   
 3 Grass Poison   525    80 Venusaur  
 4 Grass Poison   625    80 Venusaur  
 5 Fire  <NA>     309    39 Charmander
 6 Fire  <NA>     405    58 Charmeleon
 7 Fire  Flying   534    78 Charizard 
 8 Fire  Dragon   634    78 Charizard 
 9 Fire  Flying   634    78 Charizard 
10 Water <NA>     314    44 Squirtle  
# ℹ 1,516 more rows

Mutate

Create a column named n_elevator that calculates how many of these pokemon could fit in an elevator with a weight limit of 6000 lbs. You may assume that pokemon can be infinitely compressed, so that the volume of the pokemon is not a concern.

Round your answer down to the nearest integer with the floor() function - no one needs to get hurt!

Remember, you will need to convert kg to lbs, so you may want to create an additional column named weight_lbs.

Output only gen, name, and the columns you created.

poke_types |>
  mutate(weight_lbs = weight_kg * 2.204623, 
         n_elevator = floor(6000/weight_lbs)) |>
  select(gen, name, weight_lbs, n_elevator)
# A tibble: 1,526 × 4
     gen name       weight_lbs n_elevator
   <dbl> <chr>           <dbl>      <dbl>
 1     1 Bulbasaur        15.2        394
 2     1 Ivysaur          28.7        209
 3     1 Venusaur        220.          27
 4     1 Venusaur        220.          27
 5     1 Charmander       18.7        320
 6     1 Charmeleon       41.9        143
 7     1 Charizard       200.          30
 8     1 Charizard       200.          30
 9     1 Charizard       200.          30
10     1 Squirtle         19.8        302
# ℹ 1,516 more rows

Group_by + Summarize!

Work with poke_types and only worry about the primary type (type1)

Bug Pokemon by Generation

Calculate the total number of bug pokemon in each generation.

poke_types |>
  filter(type1 == "Bug") |>
  group_by(gen) |>
  summarize(Bug = n())
# A tibble: 9 × 2
    gen   Bug
  <dbl> <int>
1     1    14
2     2    12
3     3    12
4     4    24
5     5    22
6     6     3
7     7     9
8     8     4
9     9     7
# OR

poke_types |>
  group_by(gen) |>
  summarize(Bug = sum(type1=="Bug"))
# A tibble: 9 × 2
    gen   Bug
  <dbl> <int>
1     1    14
2     2    12
3     3    12
4     4    24
5     5    22
6     6     3
7     7     9
8     8     4
9     9     7

Pokemon Weight By Type

Find the heaviest pokemon of each type, making sure to remove any duplicate rows.

poke_types |>
  select(gen, name, type1, weight_kg) |> # Keep only basic info
  unique() |> # Remove duplicate rows
  group_by(type1) |>
  filter(weight_kg == max(weight_kg))
# A tibble: 18 × 4
# Groups:   type1 [18]
     gen name         type1    weight_kg
   <dbl> <chr>        <chr>        <dbl>
 1     1 Snorlax      Normal        460 
 2     3 Wailord      Water         398 
 3     3 Groudon      Ground        950 
 4     4 Torterra     Grass         310 
 5     4 Heatran      Fire          430 
 6     4 Giratina     Ghost         750 
 7     6 Noivern      Flying         85 
 8     6 Xerneas      Fairy         215 
 9     7 Cosmoem      Psychic      1000.
10     7 Buzzwole     Bug           334.
11     7 Celesteela   Steel        1000.
12     7 Guzzlord     Dark          888 
13     7 Stakataka    Rock          820 
14     8 Eternatus    Poison        950 
15     8 Glastrier    Ice           800 
16     9 Iron Hands   Fighting      381.
17     9 Roaring Moon Dragon        380 
18     9 Miraidon     Electric      240 

Pokemon HP by Generation

Find the pokemon with the highest HP for each generation. Include all tied rows.

poke_types |>
  select(gen, name, hp) |>
  unique() |>
  group_by(gen) |>
  filter(hp == max(hp)) |>
  arrange(gen)
# A tibble: 10 × 3
# Groups:   gen [9]
     gen name         hp
   <dbl> <chr>     <dbl>
 1     1 Chansey     250
 2     2 Blissey     255
 3     3 Wailord     170
 4     4 Drifblim    150
 5     4 Giratina    150
 6     5 Alomomola   165
 7     6 Zygarde     216
 8     7 Guzzlord    223
 9     8 Eternatus   255
10     9 Cetitan     170

© Copyright 2026, Susan Vanderplas

 
  • Report an issue