複数時系列データの結合

> FirstDate="2014/8/1"
> LastDate="2014/8/31"
> AllData01<-data.frame(date=seq(as.Date(FirstDate),as.Date(LastDate),by="days"),dummy=1)
> AllData02<-AllData01
> TSeriesDataFileList<-dir("C:/Users/USERNAME/Desktop/R_Folder")
> for(iii in 1:length(TSeriesDataFileList))
+ {
+  dataset<-read.table(file=TSeriesDataFileList[iii],header=TRUE,sep=",")
+  dataset[,1]<-as.Date(dataset[,1])
+  for(ccc in 2:ncol(dataset))
+  {
+   #以下は非数値データ行の削除及びデータの実数化。オリジナルデータに合わせて適宜変更。
+   dataset<-subset(dataset,dataset[,ccc]!=".")
+   dataset<-subset(dataset,is.na(dataset[,ccc])==F)
+   dataset<-subset(dataset,as.Date(FirstDate)<=dataset[,1])
+   dataset<-subset(dataset,as.Date(LastDate)>=dataset[,1])
+   dataset[,ccc]<-as.double(as.character(dataset[,ccc]))
+   #日付列名の統一
+   colnames(dataset)[1]<-"date"
+  }
+  print(head(dataset,3))
+  if(1<=iii)
+  { 
+   AllData01<-merge(AllData01,dataset,by="date",all=T,sort=T) #欠損はNAとして処理
+   AllData02<-merge(AllData02,dataset,by="date",sort=T) #全系列に数値データが存在する日付のみ
+  }
+  else
+  {
+   AllData01<-dataset
+   AllData02<-dataset
+  }
+ }
            date  VALUE
11370 2014-08-01 102.45
11371 2014-08-04 102.47
11372 2014-08-05 102.77
            date   VALUE
12090 2014-08-01 1284.50
12091 2014-08-04 1293.50
12092 2014-08-05 1292.75
          date    Close     Open     High      Low
881 2014-08-01 15523.11 15511.54 15602.13 15501.15
882 2014-08-04 15474.50 15474.65 15535.62 15440.21
883 2014-08-05 15320.31 15506.87 15507.32 15313.84
           date   VALUE
2588 2014-08-01 1925.15
2589 2014-08-04 1938.99
2590 2014-08-05 1920.21
> TSeriesDataFileList
[1] "DEXJPUS.csv"                       "GOLDAMGBD228NLBM.csv"              "nikkei_stock_average_daily_en.csv"
[4] "SP500.csv"                        
> AllData01
         date dummy VALUE.x VALUE.y    Close     Open     High      Low   VALUE
1  2014-08-01     1  102.45 1284.50 15523.11 15511.54 15602.13 15501.15 1925.15
2  2014-08-02     1      NA      NA       NA       NA       NA       NA      NA
3  2014-08-03     1      NA      NA       NA       NA       NA       NA      NA
4  2014-08-04     1  102.47 1293.50 15474.50 15474.65 15535.62 15440.21 1938.99
5  2014-08-05     1  102.77 1292.75 15320.31 15506.87 15507.32 15313.84 1920.21
6  2014-08-06     1  102.34 1288.50 15159.79 15260.00 15271.14 15121.43 1920.24
7  2014-08-07     1  102.06 1302.00 15232.37 15138.72 15243.06 15061.99 1909.57
8  2014-08-08     1  101.91 1317.50 14778.37 15063.73 15074.32 14753.84 1931.59
9  2014-08-09     1      NA      NA       NA       NA       NA       NA      NA
10 2014-08-10     1      NA      NA       NA       NA       NA       NA      NA
11 2014-08-11     1  102.14 1308.25 15130.52 15022.64 15161.54 14973.71 1936.92
12 2014-08-12     1  102.22 1311.00 15161.31 15164.73 15235.98 15125.93 1933.75
13 2014-08-13     1  102.48 1309.25 15213.63 15111.76 15235.90 15108.53 1946.72
14 2014-08-14     1  102.44 1315.00 15314.57 15284.38 15338.85 15272.90 1955.18
15 2014-08-15     1  102.19 1313.60 15318.34 15317.15 15328.34 15276.99 1955.06
16 2014-08-16     1      NA      NA       NA       NA       NA       NA      NA
17 2014-08-17     1      NA      NA       NA       NA       NA       NA      NA
18 2014-08-18     1  102.54 1302.75 15322.60 15318.50 15365.35 15303.39 1971.74
19 2014-08-19     1  102.86 1300.25 15449.79 15451.94 15476.05 15429.11 1981.60
20 2014-08-20     1  103.36 1294.50 15454.45 15485.93 15492.88 15433.72 1986.51
21 2014-08-21     1  103.78 1280.50 15586.20 15530.98 15601.99 15525.78 1992.37
22 2014-08-22     1  103.97 1281.00 15539.19 15618.05 15628.78 15524.43 1988.40
23 2014-08-23     1      NA      NA       NA       NA       NA       NA      NA
24 2014-08-24     1      NA      NA       NA       NA       NA       NA      NA
25 2014-08-25     1  103.96      NA 15613.25 15609.43 15628.61 15553.51 1997.92
26 2014-08-26     1  104.12 1286.50 15521.22 15609.64 15613.34 15519.42 2000.02
27 2014-08-27     1  103.98 1285.00 15534.82 15556.56 15588.22 15465.99 2000.12
28 2014-08-28     1  103.78 1288.00 15459.86 15482.27 15487.99 15423.90 1996.74
29 2014-08-29     1  104.00 1285.75 15424.59 15423.88 15447.81 15356.00 2003.37
30 2014-08-30     1      NA      NA       NA       NA       NA       NA      NA
31 2014-08-31     1      NA      NA       NA       NA       NA       NA      NA
> AllData02
         date dummy VALUE.x VALUE.y    Close     Open     High      Low   VALUE
1  2014-08-01     1  102.45 1284.50 15523.11 15511.54 15602.13 15501.15 1925.15
2  2014-08-04     1  102.47 1293.50 15474.50 15474.65 15535.62 15440.21 1938.99
3  2014-08-05     1  102.77 1292.75 15320.31 15506.87 15507.32 15313.84 1920.21
4  2014-08-06     1  102.34 1288.50 15159.79 15260.00 15271.14 15121.43 1920.24
5  2014-08-07     1  102.06 1302.00 15232.37 15138.72 15243.06 15061.99 1909.57
6  2014-08-08     1  101.91 1317.50 14778.37 15063.73 15074.32 14753.84 1931.59
7  2014-08-11     1  102.14 1308.25 15130.52 15022.64 15161.54 14973.71 1936.92
8  2014-08-12     1  102.22 1311.00 15161.31 15164.73 15235.98 15125.93 1933.75
9  2014-08-13     1  102.48 1309.25 15213.63 15111.76 15235.90 15108.53 1946.72
10 2014-08-14     1  102.44 1315.00 15314.57 15284.38 15338.85 15272.90 1955.18
11 2014-08-15     1  102.19 1313.60 15318.34 15317.15 15328.34 15276.99 1955.06
12 2014-08-18     1  102.54 1302.75 15322.60 15318.50 15365.35 15303.39 1971.74
13 2014-08-19     1  102.86 1300.25 15449.79 15451.94 15476.05 15429.11 1981.60
14 2014-08-20     1  103.36 1294.50 15454.45 15485.93 15492.88 15433.72 1986.51
15 2014-08-21     1  103.78 1280.50 15586.20 15530.98 15601.99 15525.78 1992.37
16 2014-08-22     1  103.97 1281.00 15539.19 15618.05 15628.78 15524.43 1988.40
17 2014-08-26     1  104.12 1286.50 15521.22 15609.64 15613.34 15519.42 2000.02
18 2014-08-27     1  103.98 1285.00 15534.82 15556.56 15588.22 15465.99 2000.12
19 2014-08-28     1  103.78 1288.00 15459.86 15482.27 15487.99 15423.90 1996.74
20 2014-08-29     1  104.00 1285.75 15424.59 15423.88 15447.81 15356.00 2003.37
> 
> 


FirstDate="2014/8/1"
LastDate="2014/8/31"
AllData01<-data.frame(date=seq(as.Date(FirstDate),as.Date(LastDate),by="days"),dummy=1)
AllData02<-AllData01
TSeriesDataFileList<-dir("C:/Users/USERNAME/Desktop/R_Folder")
for(iii in 1:length(TSeriesDataFileList))
{
 dataset<-read.table(file=TSeriesDataFileList[iii],header=TRUE,sep=",")
 dataset[,1]<-as.Date(dataset[,1])
 for(ccc in 2:ncol(dataset))
 {
  #以下は非数値データ行の削除及びデータの実数化。オリジナルデータに合わせて適宜変更。
  dataset<-subset(dataset,dataset[,ccc]!=".")
  dataset<-subset(dataset,is.na(dataset[,ccc])==F)
  dataset<-subset(dataset,as.Date(FirstDate)<=dataset[,1])
  dataset<-subset(dataset,as.Date(LastDate)>=dataset[,1])
  dataset[,ccc]<-as.double(as.character(dataset[,ccc]))
  #日付列名の統一
  colnames(dataset)[1]<-"date"
 }
 print(head(dataset,3))
 if(1<=iii)
 { 
  AllData01<-merge(AllData01,dataset,by="date",all=T,sort=T) #欠損はNAとして処理
  AllData02<-merge(AllData02,dataset,by="date",sort=T) #全系列に数値データが存在する日付のみ
 }
 else
 {
  AllData01<-dataset
  AllData02<-dataset
 }
}
TSeriesDataFileList
AllData01
AllData02