経済統計の使い方
統計初心者の社会人向けに、経済データの解説をしています。「まとめページ」をご覧くだされば、全体的な内容がわかると思います。
R

【R】CSVファイルを読み込む|fread関数を利用

さまざまな分析の出発点として、エクセルのデータを読み込むことが挙げあれます。

読み込みたいファイルをCSVファイルにして、読み込むと簡単です。

エクセルのファイルでは複数のシートがあるので、どれを読み込むかなどを指定する必要があるためです。

【R】Rのまとめ 経済統計の使い方では、統計データの入手法から分析法まで解説しています。 https://officekaisuiyoku.com...

ディレクトリーを変更する

Rを使って、エクセルからデータを読み込むとき、最初に必要なのは、ファイルの置き場所の設定です。作業するファイルの置き場所をワーキングディレクトリーといいます。

現在のワーキングディレクトリーを調べるには

getwd()

を使います。

ワーキングディレクトリーを変更するには、setwd の後に、ディレクトリー名を指定します。日本語の入ったディレクトリー名は避けた方が無難です。

setwd(“C:/Users/kaede/software/R/euro2019”)

setwd("C:/Users/kaede/software/R/euro2019")

といった形になります。

【R】ディレクトリーの確認、移動|getwdとsetwd Rを使う際、データやプログラムファイルを格納する場所を決めておく必要があります。 まずいつも使うディレクトリー(ワーキングディ...

fread関数でデータを読み込む

ワーキングディレクトリーにCSVファイルを置いておけば、データの読み込みは以下のように、ファイル名(”tia.csv”)を指定するだけで読み込めます。

tia <- fread(”tia.csv",header=TRUE,data.table=FALSE)

file=file.choose()とすると、ファイル名を指定せず、端末からファイルを選ぶようにもできます。

fread(file=file.choose(),header=TRUE,data.table=FALSE)

header=TRUEとしておくと、最初の行が見出しとして使われます。data.table=FALSEとすると、CSVファイルがデータフレームとして読み込まれます。1列名に行名を書いてる場合は自動的に行名として読み込まれます

fread関数を使うには

fread関数はdata.tableパッケージにあります。インストールしていない場合は、以下のコマンドでインストールしておき、パッケージを使えるようにします。

install.packages("data.table")
library("data.table")

一度インストールすれば、以下のコマンドを使えば、パッケージが使えるようになります。

library("data.table")

具体例

まず、ワーキングディレクトリーに移動し、data.tableパッケージを使えるようにします。installコマンドは1度行えばその後は必要ないです。

setwd("E:/Dropbox/software/R/workingdirectry")

install.packages("data.table")
library("data.table")

以下のファイルを読み込むことにします。都道府県×都道府県の行列なので、まずデータフレームで読み込んで、行列にします。

以下のコマンドで、Wmatrixというcsvファイルが読み込めます。

w <- fread(file="Wmatrix.csv",header=TRUE,data.table=FALSE)

次に行列に変換します。

as.matrix(w)

以下が表示されます。

     V1              HK AO    IW          MG     AT     YG    
 [1,] "HK"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
 [2,] "AO"            NA "0.0" "0.3333333" "0.00" "0.25" "0.00"
 [3,] "IW"            NA "0.5" "0.0000000" "0.25" "0.25" "0.00"
 [4,] "MG"            NA "0.0" "0.3333333" "0.00" "0.25" "0.25"
 [5,] "AT"            NA "0.5" "0.3333333" "0.25" "0.00" "0.25"
 [6,] "YG"            NA "0.0" "0.0000000" "0.25" "0.25" "0.00"
 [7,] "FS"            NA "0.0" "0.0000000" "0.25" "0.00" "0.25"
 [8,] "IG"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
 [9,] "TG"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[10,] "GM"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[11,] "ST"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[12,] "CB"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[13,] "TK"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[14,] "KG"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[15,] "NG"            NA "0.0" "0.0000000" "0.00" "0.00" "0.25"
[16,] "TY"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[17,] "IK"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[18,] "FI"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[19,] "YN"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
[20,] "NN"            NA "0.0" "0.0000000" "0.00" "0.00" "0.00"
      FS          IG     TG     GM    ST          CB          TK    
 [1,] "0.0000000" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
 [2,] "0.0000000" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
 [3,] "0.0000000" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
 [4,] "0.1666667" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
 [5,] "0.0000000" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
 [6,] "0.1666667" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
 [7,] "0.0000000" "0.25" "0.25" "0.2" "0.0000000" "0.0000000" "0.00"
 [8,] "0.1666667" "0.00" "0.25" "0.0" "0.1428571" "0.3333333" "0.00"
 [9,] "0.1666667" "0.25" "0.00" "0.2" "0.1428571" "0.0000000" "0.00"
[10,] "0.1666667" "0.00" "0.25" "0.0" "0.1428571" "0.0000000" "0.00"
[11,] "0.0000000" "0.25" "0.25" "0.2" "0.0000000" "0.3333333" "0.25"
[12,] "0.0000000" "0.25" "0.00" "0.0" "0.1428571" "0.0000000" "0.25"
[13,] "0.0000000" "0.00" "0.00" "0.0" "0.1428571" "0.3333333" "0.00"
[14,] "0.0000000" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.25"
[15,] "0.1666667" "0.00" "0.00" "0.2" "0.0000000" "0.0000000" "0.00"
[16,] "0.0000000" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
[17,] "0.0000000" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
[18,] "0.0000000" "0.00" "0.00" "0.0" "0.0000000" "0.0000000" "0.00"
[19,] "0.0000000" "0.00" "0.00" "0.0" "0.1428571" "0.0000000" "0.25"
[20,] "0.0000000" "0.00" "0.00" "0.2" "0.1428571" "0.0000000" "0.00"
      KG          NG    TY     IK          FI     YN    NN         
 [1,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
 [2,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
 [3,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
 [4,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
 [5,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
 [6,] "0.0000000" "0.2" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
 [7,] "0.0000000" "0.2" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
 [8,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
 [9,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
[10,] "0.0000000" "0.2" "0.00" "0.0000000" "0.00" "0.0" "0.1428571"
[11,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.2" "0.1428571"
[12,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.0000000"
[13,] "0.3333333" "0.0" "0.00" "0.0000000" "0.00" "0.2" "0.0000000"
[14,] "0.0000000" "0.0" "0.00" "0.0000000" "0.00" "0.2" "0.0000000"
[15,] "0.0000000" "0.0" "0.25" "0.0000000" "0.00" "0.0" "0.1428571"
[16,] "0.0000000" "0.2" "0.00" "0.3333333" "0.00" "0.0" "0.1428571"
[17,] "0.0000000" "0.0" "0.25" "0.0000000" "0.25" "0.0" "0.0000000"
[18,] "0.0000000" "0.0" "0.00" "0.3333333" "0.00" "0.0" "0.0000000"
[19,] "0.3333333" "0.0" "0.00" "0.0000000" "0.00" "0.0" "0.1428571"
[20,] "0.0000000" "0.2" "0.25" "0.0000000" "0.00" "0.2" "0.0000000"
      GF          SO          AC     ME          SI     KT         
 [1,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
 [2,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
 [3,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
 [4,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
 [5,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
 [6,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
 [7,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
 [8,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
 [9,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
[10,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
[11,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
[12,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
[13,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
[14,] "0.0000000" "0.3333333" "0.00" "0.0000000" "0.00" "0.0000000"
[15,] "0.0000000" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
[16,] "0.1428571" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
[17,] "0.1428571" "0.0000000" "0.00" "0.0000000" "0.00" "0.0000000"
[18,] "0.1428571" "0.0000000" "0.00" "0.0000000" "0.25" "0.1666667"
[19,] "0.0000000" "0.3333333" "0.00" "0.0000000" "0.00" "0.0000000"
[20,] "0.1428571" "0.0000000" "0.25" "0.0000000" "0.00" "0.0000000"
      OS     HG     NR     WY          TT     SN          OY         
 [1,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
 [2,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
 [3,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
 [4,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
 [5,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
 [6,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
 [7,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
 [8,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
 [9,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[10,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[11,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[12,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[13,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[14,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[15,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[16,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[17,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[18,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[19,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
[20,] "0.00" "0.00" "0.00" "0.0000000" "0.00" "0.0000000" "0.0000000"
      HS     YI    TS          KW  EM    KC    FO          SG    NS 
 [1,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
 [2,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
 [3,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
 [4,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
 [5,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
 [6,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
 [7,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
 [8,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
 [9,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[10,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[11,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[12,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[13,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[14,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[15,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[16,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[17,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[18,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[19,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
[20,] "0.00" "0.0" "0.0000000" "0" "0.0" "0.0" "0.0000000" "0.0" "0"
      KM     OT          MZ          KS    ON
 [1,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [2,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [3,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [4,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [5,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [6,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [7,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [8,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [9,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[10,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[11,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[12,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[13,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[14,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[15,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[16,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[17,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[18,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[19,] "0.00" "0.0000000" "0.0000000" "0.0" NA
[20,] "0.00" "0.0000000" "0.0000000" "0.0" NA
 [ reached getOption("max.print") -- omitted 28 rows ]

COMMENT

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA