简体   繁体   English

无论如何,我要使用R读取6个excel神奇宝贝excel表中的多个.csv文件?

[英]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 (eg attack, speed, defense etc.) per given type of pokemon (eg water, psychic, etc.). 我正在尝试在R中练习编程功能。我做了一个函数,可以让我确定每种给定类型的宠物小精灵(例如水,通灵等)对于每种属性(例如攻击,速度,防御等)是最佳的宠物小精灵。 )。 So far, I am only able to do this for one pokemon generation (reflected in one excel file). 到目前为止,我只能在一代神奇宝贝中做到这一点(反映在一个excel文件中)。 I want to do the same to include all the 6 generations (stored in 6 excel files). 我想做同样的事情以包含所有6代(存储在6个excel文件中)。 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: 这是我目前在R中的上述功能代码:

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)   }

First thing, I believe that you can simplify your code a little, especially the first when you define the objects data and dx. 首先,我相信您可以稍微简化代码,尤其是在定义对象数据和dx时。

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

You can use F or T instead of False or True. 您可以使用F或T代替False或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. read.csv()函数具有stringsAsFactors参数,因此所有字符列都将被读取为字符,而数字列将保持数字状态。 The header parameter in the function is True by default. 该函数中的header参数默认为True。 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. 同样,read.csv()函数的输出通常是一个data.frame,因此您此时不必使用as.data.frame函数。 For more information run "?read.csv" in your R console. 有关更多信息,请在R控制台中运行“?read.csv”。 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 . 现在,参考其他世代,我认为您可以在某些条件下练习, if else First install the xlsx package and readxl package so you don't have to necessarily export your excel files to csv format. 首先安装xlsx软件包和readxl软件包,这样就不必将Excel文件导出为csv格式。 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: 例如,您可以尝试添加新的参数“ gen”或“ generation”,该参数需要输入1到6之间的数字,并执行以下操作:

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! 祝好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM