-
Notifications
You must be signed in to change notification settings - Fork 62
/
readAndWritePokemonData.R
50 lines (42 loc) · 1.39 KB
/
readAndWritePokemonData.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# read and write updated Pokemon data
#
library(readxl)
thePokemon <- read_xlsx("Pokemon.xlsx")
write.csv(thePokemon,file="Pokemon.csv",row.names=FALSE)
# starting with Pokemon.csv instead of xlsx
thePokemon <- read.csv("Pokemon.csv")
library(writexl)
write_xlsx(thePokemon,"Pokemon.xlsx")
# build single spreadsheet from genXX.csv files
thePokemon <- do.call(rbind,lapply(1:8,function(x){
read.csv(sprintf("gen%02d.csv",x))
}))
# write spreadsheet to csv
write.csv(thePokemon,file="Pokemon.csv",row.names=FALSE)
# extract & write individual csv files from single Pokemon file
generations <- 1:8
lapply(generations,function(x){
data <- thePokemon[thePokemon$Generation == x,]
write.csv(data,
sprintf("gen%02d.csv",x),
row.names=FALSE)
})
# create zip file
fileList <- unlist(lapply(generations,function(x){
sprintf("gen%02d.csv",x)
}))
if(file.exists("PokemonData.zip")) file.remove("PokemonData.zip")
zip("PokemonData.zip",fileList)
# write as individual spreadsheets and zip
# write single spreadsheet from genXX.csv files
library(writexl)
thePokemon <- do.call(rbind,lapply(1:8,function(x){
df <-read.csv(sprintf("gen%02d.csv",x))
write_xlsx(df,path=sprintf("gen%02d.xlsx",x))
}))
# create zip file
fileList <- unlist(lapply(1:8,function(x){
sprintf("gen%02d.xlsx",x)
}))
if(file.exists("PokemonXLSX.zip")) file.remove("PokemonXLSX.zip")
zip("PokemonXLSX.zip",fileList)