|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
library(rvest) |
|
pages <- 1:9 |
|
baseURL <- "https://pokemondb.net/pokedex/stats/gen" |
|
|
|
pokemonList <- lapply(pages,function(x){ |
|
html <- read_html(paste0(baseURL,x)) |
|
tableNode <- html_node(html,xpath="//*[(@id = 'pokedex')]") |
|
data <- html_table(html,header=TRUE)[[1]] |
|
colnames(data)[1] <- "ID" |
|
|
|
data$Generation <- x |
|
data |
|
}) |
|
names(pokemonList) <- paste0("gen",pages) |
|
|
|
thePokemon <- do.call(rbind,pokemonList) |
|
|
|
|
|
|
|
|
|
types <- strsplit(gsub('[a-z]\\K(?=[A-Z])', ' ',thePokemon$Type, perl=T)," ") |
|
thePokemon$Type1 <- unlist(lapply(types,function(x) x[1])) |
|
thePokemon$Type2 <- unlist(lapply(types,function(x) ifelse(is.na(x[2])," ",x[2]))) |
|
pokemonNames <- strsplit(gsub('[a-z]\\K(?=[A-Z])', '--', thePokemon$Name, perl=T),"--") |
|
thePokemon$Name <- unlist(lapply(pokemonNames,function(x) x[1])) |
|
thePokemon$Form <- unlist(lapply(pokemonNames,function(x) ifelse(is.na(x[2])," ",x[2]))) |
|
|
|
|
|
thePokemon <- thePokemon[,c(1,2,14,12,13,4,5,6,7,8,9,10,11)] |
|
thePokemon[29,"Name"] <- "Nidoran" |
|
thePokemon[29,"Form"] <- "Female" |
|
thePokemon[32,"Name"] <- "Nidoran" |
|
thePokemon[32,"Form"] <- "Male" |
|
|
|
|
|
write.csv(thePokemon,file="Pokemon.csv",row.names=FALSE) |
|
|
|
|
|
library(writexl) |
|
write_xlsx(thePokemon,"Pokemon.xlsx") |
|
|
|
|
|
generations <- 1:9 |
|
lapply(generations,function(x){ |
|
data <- thePokemon[thePokemon$Generation == x,] |
|
write.csv(data, |
|
sprintf("gen%02d.csv",x), |
|
row.names=FALSE) |
|
}) |
|
|
|
|
|
fileList <- unlist(lapply(generations,function(x){ |
|
sprintf("gen%02d.csv",x) |
|
})) |
|
if(file.exists("PokemonData.zip")) file.remove("PokemonData.zip") |
|
zip("PokemonData.zip",fileList) |
|
|