读取位于R

时间:2018-12-08 21:49:43

标签: r read.csv

您好,我正在尝试读取位于同一目录中的多个csv文件。我想选择工作目录,然后将所有文件读入一个大列表(如果可能)。我的尝试在下面。任何帮助将不胜感激。我不知道我在做什么错!

directory <- dlgDir()

file_list <- list.files(path = "directory", pattern = "*.csv")

bigList <- sapply(file_list, read.csv)

1 个答案:

答案 0 :(得分:0)

这里是一个示例,该示例使用kaggle.com的Alberto Barradas的Pokémon Stats数据的更新版本,该数据从目录中读取文件列表并将其组合为数据框。

download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
              "pokemonData.zip",
              method="curl",mode="wb")
unzip("pokemonData.zip")

thePokemonFiles <- list.files("./pokemonData",
                                  full.names=TRUE)
thePokemonFiles 

pokemonData <- lapply(thePokemonFiles,function(x) read.csv(x))

在这一点上,对象pokemonData是七个数据帧的列表,包含七个神奇宝贝世代之一,我们将用summary()进行演示。

    > summary(pokemonData)
     Length Class      Mode
[1,] 13     data.frame list
[2,] 13     data.frame list
[3,] 13     data.frame list
[4,] 13     data.frame list
[5,] 13     data.frame list
[6,] 13     data.frame list
[7,] 13     data.frame list

要将它们组合成单个数据帧,我们将do.call()rbind()函数一起使用。

pokemonData <- do.call(rbind,pokemonData)

为证明pokemonData对象现在由具有所有七代神奇宝贝的单个数据帧组成,我们将再次执行summary()

> summary(pokemonData)
     Number                             Name    
 Min.   :  1.0   Abra                     :  1  
 1st Qu.:208.0   Aerodactyl               :  1  
 Median :402.0   AerodactylMega Aerodactyl:  1  
 Mean   :405.4   Alakazam                 :  1  
 3rd Qu.:609.0   AlakazamMega Alakazam    :  1  
 Max.   :807.0   Arbok                    :  1  
                 (Other)                  :887  
     Type1         Type2         Total             HP        
 Water  :122          :385   Min.   :175.0   Min.   :  1.00  
 Normal :110   Flying :108   1st Qu.:330.0   1st Qu.: 50.00  
 Grass  : 82   Ground : 37   Median :455.0   Median : 66.00  
 Bug    : 78   Poison : 35   Mean   :437.6   Mean   : 69.44  
 Psychic: 66   Psychic: 35   3rd Qu.:518.0   3rd Qu.: 80.00  
 Fire   : 58   (Other):258   Max.   :780.0   Max.   :255.00  
 (Other):377   NA's   : 35                                   
     Attack          Defense         SpecialAtk   
 Min.   :  5.00   Min.   :  5.00   Min.   : 10.0  
 1st Qu.: 55.00   1st Qu.: 50.00   1st Qu.: 50.0  
 Median : 75.00   Median : 70.00   Median : 65.0  
 Mean   : 79.83   Mean   : 74.39   Mean   : 73.4  
 3rd Qu.:100.00   3rd Qu.: 90.00   3rd Qu.: 95.0  
 Max.   :190.00   Max.   :230.00   Max.   :194.0  

   SpecialDef         Speed          Generation   
 Min.   : 20.00   Min.   :  5.00   Min.   :1.000  
 1st Qu.: 50.00   1st Qu.: 45.00   1st Qu.:2.000  
 Median : 70.00   Median : 65.00   Median :4.000  
 Mean   : 72.37   Mean   : 68.21   Mean   :3.713  
 3rd Qu.: 90.00   3rd Qu.: 90.00   3rd Qu.:5.000  
 Max.   :230.00   Max.   :180.00   Max.   :7.000  

 Legendary  
 False:734  
 True : 65  
 NA's : 94