Pokémon

Data exploring assignment

Question 1

What are the first 10 rows of the dataset? What are the last 7 rows?

Process:

first_10 <- head(pkm,10)
last_7 <- tail(pkm,7)
#Answer:
#The first 10 rows:
first_10
# A tibble: 10 × 29
   id       set   series publisher generation release_date artist  name  set_num
   <chr>    <chr> <chr>  <chr>     <chr>      <chr>        <chr>   <chr> <chr>  
 1 base1-1  Base  Base   WOTC      First      1/9/1999     Ken Su… Alak… 1      
 2 base1-2  Base  Base   WOTC      First      1/9/1999     Ken Su… Blas… 2      
 3 base1-3  Base  Base   WOTC      First      1/9/1999     Ken Su… Chan… 3      
 4 base1-4  Base  Base   WOTC      First      1/9/1999     Mitsuh… Char… 4      
 5 base1-5  Base  Base   WOTC      First      1/9/1999     Ken Su… Clef… 5      
 6 base1-6  Base  Base   WOTC      First      1/9/1999     Mitsuh… Gyar… 6      
 7 base1-7  Base  Base   WOTC      First      1/9/1999     Ken Su… Hitm… 7      
 8 base1-8  Base  Base   WOTC      First      1/9/1999     Ken Su… Mach… 8      
 9 base1-9  Base  Base   WOTC      First      1/9/1999     Keiji … Magn… 9      
10 base1-10 Base  Base   WOTC      First      1/9/1999     Ken Su… Mewt… 10     
# ℹ 20 more variables: types <chr>, supertype <chr>, subtypes <chr>,
#   level <chr>, hp <dbl>, evolves_from <chr>, evolves_to <chr>,
#   abilities <chr>, attacks <chr>, weaknesses <chr>, retreat_cost <chr>,
#   converted_retreat_cost <dbl>, rarity <chr>, flavor_text <chr>,
#   national_pokedex_numbers <chr>, legalities <chr>, resistances <chr>,
#   rules <chr>, regulation_mark <chr>, ancient_trait <chr>
#The last 7 rows:
last_7
# A tibble: 7 × 29
  id      set      series publisher generation release_date artist name  set_num
  <chr>   <chr>    <chr>  <chr>     <chr>      <chr>        <chr>  <chr> <chr>  
1 sv4-260 Paradox… Scarl… TPCI      Ninth      11/3/2023    5ban … Garc… 260    
2 sv4-261 Paradox… Scarl… TPCI      Ninth      11/3/2023    aky C… Iron… 261    
3 sv4-262 Paradox… Scarl… TPCI      Ninth      11/3/2023    takuy… Roar… 262    
4 sv4-263 Paradox… Scarl… TPCI      Ninth      11/3/2023    Oswal… Beac… 263    
5 sv4-264 Paradox… Scarl… TPCI      Ninth      11/3/2023    Toyst… Coun… 264    
6 sv4-265 Paradox… Scarl… TPCI      Ninth      11/3/2023    Toyst… Luxu… 265    
7 sv4-266 Paradox… Scarl… TPCI      Ninth      11/3/2023    <NA>   Reve… 266    
# ℹ 20 more variables: types <chr>, supertype <chr>, subtypes <chr>,
#   level <chr>, hp <dbl>, evolves_from <chr>, evolves_to <chr>,
#   abilities <chr>, attacks <chr>, weaknesses <chr>, retreat_cost <chr>,
#   converted_retreat_cost <dbl>, rarity <chr>, flavor_text <chr>,
#   national_pokedex_numbers <chr>, legalities <chr>, resistances <chr>,
#   rules <chr>, regulation_mark <chr>, ancient_trait <chr>

Question 2

What is the average hp of all Pokémons?

Process:

hp_mean <- mean(pkm$hp, na.rm = TRUE)
hp_mean
[1] 104.8397

Answer:

The average hp of all Pokémons is 104.8397083

Question 3

What type of object is “type” ?

Process:

type <- mode(pkm$types)
type
[1] "character"

Answer:

“type” is a character nested list type object. It is a nominal variable.

Question 4

How many unique types of Pokémon are there?

Process:

types_num <- length(unique(pkm$types))
types_num
[1] 37

Answer:

There are 37 unique types of Pokémon

Question 5

What are the mean, 25 percentile, and 75 percentile of Pokémon hp?

Process:

quarts <- quantile(pkm$hp,probs = c(0.25, 0.5, 0.75), na.rm = TRUE)
quarts
25% 50% 75% 
 60  80 130 
mean_hp <- mean(pkm$hp, na.rm = TRUE)
mean_hp
[1] 104.8397

Answer:

The 25 percentile of Pokémon hp is 60.

The 50 percentile of Pokémon hp is 80.

The 75 percentile of Pokémon hp is 130.

The mean is 104.8397083

Question 6

What are the top 3 most popular types of Pokémon in the cards and how many of each are there?

Process:

table_ <- table(pkm$types)
sorted_table <- sort(table_, decreasing = TRUE)

top1_num <- sorted_table[1]
top2_num <- sorted_table[2]
top3_num <- sorted_table[3]
top1 <- names(top1_num)
top2 <- names(top2_num)
top3 <- names(top3_num)
top1_num
['Water'] 
     2088 
top2_num
['Grass'] 
     2010 
top3_num
['Psychic'] 
       1941 

Answer:

The most popular three types are [‘Water’],[‘Grass’], [‘Psychic’]

[‘Water’] has 2088 cards.

[‘Grass’] has 2010 cards.

[‘Psychic’] has 1941 cards.

Question 7

What is the least popular type of Pokémon? How many cards does it have?

min_num <- min(sorted_table)
li <-list()
for (i in 1: length(sorted_table))
     {
       if(sorted_table[[i]] == min_num)
       {
         name <- names(sorted_table)[i]
         li <- append(li, name)
       }
}
least_num <- min_num
least_name1 <-li[1]
least_name2 <-li[2]
least_name3 <-li[3]
least_name1
[[1]]
[1] "['Fairy', 'Water']"
least_name2
[[1]]
[1] "['Fire', 'Grass']"
least_name3
[[1]]
[1] "['Lightning', 'Grass']"

The least popular type of Pokémon are [‘Fairy’, ‘Water’], [‘Fire’, ‘Grass’], [‘Lightning’, ‘Grass’]. They all have only 1 card.

Question 8

How many null values do we have in “types” of Pokémon?

Process:

null_type_num <- sum(is.na(pkm$types))
null_type_num
[1] 2672

Answer:

There are 2672 null values in “types” of Pokémon

Question 9

Do a very quick exploratory histogram of types of Pokémon. Is this visualization useful?

Process:

hist(table(pkm$types))

ggplot(pkm,aes(x = types))+
  geom_bar()

Answer:

This graph is not valuable, because the most types have the same amount of frequency.And it just shows the count of each type. The graphs does not really show any trend.

Question 10

Freestyle: Search for something else with the data! What did you find?

Process: I would like to visualize the relationship between hp and level.

ggplot(pkm,aes(x = level, 
               y = hp))+
  geom_point()
Warning: Removed 2636 rows containing missing values (`geom_point()`).

Answer:

Even if the diagram doesn’t look pretty, there is a little trend that the higher the level is, the higher the hp would be. And the highest level has the highest hp Pokémon.

Data Wrangling assignment

Question 1 - Question 2

  1. There are a ton of variables, but we don’t want that many. Make a new data frame or table containing only the following variables. You can choose to make a new one or drop the rest of the columns using drop function. Include these variables:Generation , release_date, Artist, Name, Types, supertypes, Hp, evolvesFrom, evolvesTo, Nationalpokedexnumbers

  2. Show the last 10 rows of your new table or data frame.

Process:

pkm$id <- NULL
pkm$set <- NULL
pkm$series <- NULL
pkm$publisher <- NULL
pkm$set_num <- NULL
pkm$subtypes <- NULL
pkm$level <- NULL
pkm$abilities <- NULL
pkm$attacks <- NULL
pkm$weaknesses <- NULL
pkm$retreat_cost <- NULL
pkm$converted_retreat_cost <- NULL
pkm$rarity <- NULL
pkm$flavor_text <- NULL
pkm$legalities <- NULL
pkm$resistances <- NULL
pkm$rules <- NULL
pkm$regulation_mark <- NULL
pkm$ancient_trait <- NULL

row_10 <- tail(pkm, 10)
row_10
# A tibble: 10 × 10
   generation release_date artist       name  types supertype    hp evolves_from
   <chr>      <chr>        <chr>        <chr> <chr> <chr>     <dbl> <chr>       
 1 Ninth      11/3/2023    Atsushi Fur… Prof… <NA>  Trainer      NA <NA>        
 2 Ninth      11/3/2023    kantaro      Rika  <NA>  Trainer      NA <NA>        
 3 Ninth      11/3/2023    AKIRA EGAWA  Tulip <NA>  Trainer      NA <NA>        
 4 Ninth      11/3/2023    5ban Graphi… Garc… ['Wa… Pokémon     320 Gabite      
 5 Ninth      11/3/2023    aky CG Works Iron… ['Ps… Pokémon     220 <NA>        
 6 Ninth      11/3/2023    takuyoa      Roar… ['Da… Pokémon     230 <NA>        
 7 Ninth      11/3/2023    Oswaldo KATO Beac… <NA>  Trainer      NA <NA>        
 8 Ninth      11/3/2023    Toyste Beach Coun… <NA>  Trainer      NA <NA>        
 9 Ninth      11/3/2023    Toyste Beach Luxu… <NA>  Trainer      NA <NA>        
10 Ninth      11/3/2023    <NA>         Reve… <NA>  Energy       NA <NA>        
# ℹ 2 more variables: evolves_to <chr>, national_pokedex_numbers <chr>

Question 3

What are supertypes? How many unique supertypes of pokémon do we have?

Process:

uni_supertypes <- length(unique(pkm$supertype))
uni_supertypes
[1] 3

Answer:

Supertypes represents cards’ supertype, like Pokémon, Trainer and Energy. Thus, there are only 3 unique supertypes of pokemon.

Question 4

Do we have any null values in the table? Do we have any null values in hp?

Process:

na_hp <- sum(is.na(pkm$hp))
na_hp
[1] 2636

Answer:

Yes, we do have null values in the table. There are2636 null values in hp.

Question 5

Extract null hp into a new dataframe. What kinds of supertypes have null hp?

Process:

na_hp <- is.na(pkm$hp)
na_row_hp <- pkm[na_hp, ]
na_df <- data.frame("null_hp" = na_row_hp)
na_hp_supertype <- unique(na_df$null_hp.supertype)
na_hp_supertype
[1] "Trainer" "Energy" 

Answer:

The null hps have Trainer, Energy.

Question 6

Go back to the (non-null) dataframe and wrangle the data to eliminate non-Pokémon supertypes in your data frame. Check to see if you have only pokemon supertypes now.

Process:

pkm <-
  pkm |> 
  filter(supertype == "Pokémon")
unique(pkm$supertype)
[1] "Pokémon"
  1. Great! Now that we only have pokémon, delete the supertype column. After deleting it, check to see if we have any null values in hp. (That would be an issue if our pokémon have no hp :o). What did you find?

Process:

pkm$supertype <- NULL
sum(is.na(pkm$hp))
[1] 0

Answer:

There is no null values in hp.

Question 8

Notice the generation variable. What type of data is that (nominal, ordinal, ratio, etc)? What type of data is it in the data frame (int, float, string,..)? What unique values do we have?

Process:

uni_generation <- unique(pkm$generation)
uni_generation
 [1] "First"   "Second"  "Third"   "Fourth"  "Other"   "Fifth"   "Sixth"  
 [8] "Seventh" "Eighth"  "Ninth"  

Answer:

Generation is an ordinal and string variable.

It’s unique values are First, Second, Third, Fourth, Other, Fifth, Sixth, Seventh, Eighth, Ninth.

Question 9

Wrangle the values of generations so we have one single category per generation.

Process:

uni_generation
 [1] "First"   "Second"  "Third"   "Fourth"  "Other"   "Fifth"   "Sixth"  
 [8] "Seventh" "Eighth"  "Ninth"  

Answer:

The data is clean. Only one category for each generation

Question 10

Transform the generation variable into a numerical (categorical)value. (HintLinks to an external site.). Delete the Generation column afterwards.

Process:

pkm<-
  pkm |>
  mutate(generation_int = case_when(generation == "First" ~ 1,
                                     generation == "second" ~ 2,
                                     generation == "Third" ~ 3,
                                     generation == "Fourth" ~ 4,
                                     generation == "Fifth" ~ 5,
                                     generation == "Sixth"~ 6,
                                     generation == "Seventh" ~ 7,
                                     generation == "Eighth" ~ 8,
                                     generation == "Ninth" ~ 9,
                                     generation == "Other" ~ 10))
pkm$generation <- NULL
glimpse(pkm)
Rows: 14,497
Columns: 9
$ release_date             <chr> "1/9/1999", "1/9/1999", "1/9/1999", "1/9/1999…
$ artist                   <chr> "Ken Sugimori", "Ken Sugimori", "Ken Sugimori…
$ name                     <chr> "Alakazam", "Blastoise", "Chansey", "Charizar…
$ types                    <chr> "['Psychic']", "['Water']", "['Colorless']", …
$ hp                       <dbl> 80, 100, 120, 120, 40, 100, 70, 100, 60, 60, …
$ evolves_from             <chr> "Kadabra", "Wartortle", NA, "Charmeleon", NA,…
$ evolves_to               <chr> NA, NA, "['Blissey']", NA, "['Clefable']", NA…
$ national_pokedex_numbers <chr> "[65]", "[9]", "[113]", "[6]", "[35]", "[130]…
$ generation_int           <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …

Question 11

Query for your favorite pokémon and show us your result! Display your result. What do you notice?

Process:

raichu <- subset(pkm, name == "Raichu")
raichu
# A tibble: 41 × 9
   release_date artist           name   types         hp evolves_from evolves_to
   <chr>        <chr>            <chr>  <chr>      <dbl> <chr>        <chr>     
 1 1/9/1999     Ken Sugimori     Raichu ['Lightni…    80 Pikachu      <NA>      
 2 10/10/1999   Mitsuhiro Arita  Raichu ['Lightni…    90 Pikachu      <NA>      
 3 10/10/1999   Mitsuhiro Arita  Raichu ['Lightni…    90 Pikachu      <NA>      
 4 2/24/2000    Ken Sugimori     Raichu ['Lightni…    80 Pikachu      <NA>      
 5 9/21/2001    Masako Yamashita Raichu ['Lightni…    80 Pikachu      <NA>      
 6 9/15/2002    Atsuko Nishida   Raichu ['Lightni…    80 Pikachu      <NA>      
 7 9/15/2002    Atsuko Nishida   Raichu ['Lightni…    80 Pikachu      <NA>      
 8 5/12/2003    Yuka Morii       Raichu ['Lightni…    80 Pikachu      <NA>      
 9 5/12/2003    Yuka Morii       Raichu ['Lightni…    80 Pikachu      <NA>      
10 3/1/2004     Ken Sugimori     Raichu ['Lightni…    80 Pikachu      <NA>      
# ℹ 31 more rows
# ℹ 2 more variables: national_pokedex_numbers <chr>, generation_int <dbl>

Answer:

Raichu’s hp was only enhanced in generation 2 and 3. From generation 4, it went back to 80 from 90. I guess it may because of the game balance.