期間の異なる時系列データの結合

> data1<-read.table(file="NIKKEI.csv",header=TRUE,sep=",")

> data2<-read.table(file="SP500.csv",header=TRUE,sep=",")

> head(data1)
       date   nikkei
1  2011/1/4 10398.10
2  2011/1/5 10380.77
3  2011/1/6 10529.76
4  2011/1/7 10541.04
5 2011/1/11 10510.68
6 2011/1/12 10512.80

> head(data2)
       date   sp500
1 2004/8/30 1099.15
2 2004/8/31 1104.24
3  2004/9/1 1105.91
4  2004/9/2 1118.31
5  2004/9/3 1113.63
6  2004/9/6       .

> data2<-subset(data2,data2[,2]!=".")

> head(data2)
       date   sp500
1 2004/8/30 1099.15
2 2004/8/31 1104.24
3  2004/9/1 1105.91
4  2004/9/2 1118.31
5  2004/9/3 1113.63
7  2004/9/7  1121.3

> data3<-merge(data1,data2,by="date",sort=F)

> head(data3)
       date   nikkei   sp500
1  2011/1/4 10398.10  1270.2
2  2011/1/5 10380.77 1276.56
3  2011/1/6 10529.76 1273.85
4  2011/1/7 10541.04  1271.5
5 2011/1/11 10510.68 1274.48
6 2011/1/12 10512.80 1285.96

> data3[,1]<-as.Date(data3[,1])

> head(data3)
        date   nikkei   sp500
1 2011-01-04 10398.10  1270.2
2 2011-01-05 10380.77 1276.56
3 2011-01-06 10529.76 1273.85
4 2011-01-07 10541.04  1271.5
5 2011-01-11 10510.68 1274.48
6 2011-01-12 10512.80 1285.96

> par(mfrow=c(2,1))

> plot(data3[,1],data3[,2],type="l",xlab="date",ylab="Nikkei")

> plot(data3[,1],data3[,3],type="l",xlab="date",ylab="SP500")
> 

20140902_01