3
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rで複数のcsvファイルを読み込む

Last updated at Posted at 2019-03-23

フォルダ内の連番になっているcsvファイルをRで一気に読み込み、それぞれ別のデータフレームに入れたい。(例えば年代別とか都道府県別とか)

001_01.csv~001_10.csv

この10個のファイルをまとめて読み込む

setwd("C:/データがある場所")
files <- list.files(path = "C:/データがある場所",full.names = T)

for(i in 1:length(files)){
  i <- formatC(i, width = 2, flag = "0") #iを01~10にする
  df <- paste("001_", i, ".csv", sep = "") #dfが001_01.csv~001_10.csvと変化するようにする
  eval(parse(text=paste("data", i, "<-read.csv('", df, "')", sep=""))) #evalで文字列を実行する
}

実行するとdata01001_01.csvのデータ、data02001_02.csvのデータって感じになる。
もっとスマートな方法あるかも

3
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?