Dcroix
Dcroix

Reputation: 1

Anyway for me to read multiple .csv files in the 6 excel pokemon excel sheet using R?

I am trying to practice programming functions in R. I have made a function that allows me to determine which is the best pokemon for each attribute (e.g. attack, speed, defense etc.) per given type of pokemon (e.g. water, psychic, etc.). So far, I am only able to do this for one pokemon generation (reflected in one excel file). I want to do the same to include all the 6 generations (stored in 6 excel files). I have been working on the code for sometime... Maybe anybody here can give some inputs? Here is my current code in R for the said function:

bestpoke<-function(Type1, attri){
    data <- read.csv("gen01.csv", colClasses = "character", header=TRUE)
    dx   <- as.data.frame(cbind(data[, 2],   # Name
                                data[, 3],   # Type1
                                data[, 6],   # HP
                                data[, 7],   # Attack
                                data[, 8],   # Defense
                                data[, 9],   # SpecialAtk
                                data[, 10],  # SpecialDef
                                data[, 11]), # Speed
                          stringsAsFactors = FALSE)
    colnames(dx) <- c("Name", "Type1", "HP", "Attack", "Defense", "SpecialAtk","SpecialDef", "Speed")
    ## Check that name and attributes are valid
    if(!Type1 %in% dx[, "Type1"]){
      stop('invalid Type')
    } else if(!attri %in% c("HP", "Attack", "Defense", "SpecialAtk", "SpecialDef", "Speed")){
      stop('invalid attribute')
    } else {
      da <- which(dx[, "Type1"] == Type1)
      db <- dx[da, ]    # extracting data for the called state
      dc <- as.numeric(db[, eval(attri)])
      max_val <- max(dc, na.rm = TRUE)
      result  <- db[, "Name"][which(dc == max_val)]
      output  <- result[order(result)]
    }
    return(output)   }

Upvotes: 0

Views: 63

Answers (1)

Alfredo Ascanio
Alfredo Ascanio

Reputation: 81

First thing, I believe that you can simplify your code a little, especially the first when you define the objects data and dx.

data <- read.csv("gen01.csv", stringsAsFactors = F)

You can use F or T instead of False or True. The read.csv() function has the stringsAsFactors parameter, so all character columns will be read as characters and the numeric columns will remain numeric. The header parameter in the function is True by default. Also, the output of the read.csv() function generally is a data.frame, so you don't have to use the as.data.frame function at that point. For more information run "?read.csv" in your R console. Then, you can use the following to subset the columns that you want:

dx <- data[, c(2, 3, 6:11)]

colnames(dx) <- c("Name", "Type1", "HP", "Attack", "Defense", "SpecialAtk","SpecialDef", "Speed")
#This last line is just fine if you want to change the names of the columns.
#You can use the function names() instead, but I think that they work the same way.

Now, referring to the other generations, I think that you can practice with some conditionals if else. First install the xlsx package and readxl package so you don't have to necessarily export your excel files to csv format. You can try adding a new parameter "gen" or "generation" that requires a numeric input from 1 to 6, for example, and do something like this:

bestpoke<-function(Type1, attri, gen = 1){ # First generation will be the default

require(readxl)

if(gen == 1) {
data <- read.csv("gen01.csv", stringsAsFactors = F)
} else if(gen == 2) {
data <- read_xls("gen02.xls", stringsAsFactors = F)
} else if(gen == 3) { } # And so on...

dx <- data[, c(2, 3, 6:11)]

colnames(dx) <- c("Name", "Type1", "HP", "Attack", "Defense", "SpecialAtk","SpecialDef", "Speed")
## From this point your function is the same

This is assuming that all the files have the same structure (number and order and type of columns).

You may want to try some other things, but if you are learning, it's better that you search for yourself some ways to accomplish them. This could be:

  • Reading and merging two or more generations (if the question is what is the best pokemon between two or more generations). In this case, the conditionals have to change.

Good luck!

Upvotes: 1

Related Questions