時系列データの結合、列の追加、条件によりデータの変更

> setwd("C:/Users/USERNAME/Desktop/R_Folder")

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

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

> head(dataset3)
       date     mb mbyy
1  2009/9/1 119996 37.5
2 2009/10/1 127196 43.8
3 2009/11/1 119375 43.4
4 2009/12/1 144903 53.6
5  2010/1/1 156860 43.4
6  2010/2/1 148834 15.3

> head(dataset4)
      date     mb  mbyy
1 2013/1/1 435197  31.2
2 2013/2/1 424196  51.2
3 2013/3/1 473674  72.2
4 2013/4/1 619433  70.6
5 2013/5/1 663050 108.1
6 2013/6/1 761590 114.5

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

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

> head(dataset3)
        date     mb mbyy
1 2009-09-01 119996 37.5
2 2009-10-01 127196 43.8
3 2009-11-01 119375 43.4
4 2009-12-01 144903 53.6
5 2010-01-01 156860 43.4
6 2010-02-01 148834 15.3

> head(dataset4)
        date     mb  mbyy
1 2013-01-01 435197  31.2
2 2013-02-01 424196  51.2
3 2013-03-01 473674  72.2
4 2013-04-01 619433  70.6
5 2013-05-01 663050 108.1
6 2013-06-01 761590 114.5

> dataset5<-merge(dataset3,dataset4,all=TRUE)

> dataset6<-cbind(dataset5,data.frame(group=1))

> head(dataset6)
        date     mb mbyy group
1 2009-09-01 119996 37.5     1
2 2009-10-01 127196 43.8     1
3 2009-11-01 119375 43.4     1
4 2009-12-01 144903 53.6     1
5 2010-01-01 156860 43.4     1
6 2010-02-01 148834 15.3     1

> tmp<-as.Date("2013/1/1")

> for(iii in 1:nrow(dataset6)){if (dataset6[,1][iii]<=tmp){dataset6[,4][iii]<-"M"}else{dataset6[,4][iii]<-"J"}}

> dataset6
         date      mb  mbyy group
1  2009-09-01  119996  37.5     M
2  2009-10-01  127196  43.8     M
3  2009-11-01  119375  43.4     M
4  2009-12-01  144903  53.6     M
5  2010-01-01  156860  43.4     M
6  2010-02-01  148834  15.3     M
7  2010-03-01  151287  13.0     M
8  2010-04-01  167404  14.6     M
9  2010-05-01  166680  22.9     M
10 2010-06-01  161079  21.7     M
11 2010-07-01  173788  39.8     M
12 2010-08-01  170379  32.7     M
13 2010-09-01  166128  38.4     M
14 2010-10-01  175564  38.0     M
15 2010-11-01  175848  47.3     M
16 2010-12-01  197892  36.6     M
17 2011-01-01  195315  24.5     M
18 2011-02-01  183568  23.3     M
19 2011-03-01  285498  88.7     M
20 2011-04-01  374003 123.4     M
21 2011-05-01  304210  82.5     M
22 2011-06-01  304710  89.2     M
23 2011-07-01  301126  73.3     M
24 2011-08-01  305702  79.4     M
25 2011-09-01  307103  84.9     M
26 2011-10-01  324157  84.6     M
27 2011-11-01  350151  99.1     M
28 2011-12-01  319111  61.3     M
29 2012-01-01  331728  69.8     M
30 2012-02-01  280484  52.8     M
31 2012-03-01  275106  -3.6     M
32 2012-04-01  363191  -2.9     M
33 2012-05-01  318611   4.7     M
34 2012-06-01  355032  16.5     M
35 2012-07-01  380501  26.4     M
36 2012-08-01  360747  18.0     M
37 2012-09-01  391947  27.6     M
38 2012-10-01  428428  32.2     M
39 2012-11-01  388277  10.9     M
40 2012-12-01  435567  36.5     M
41 2013-01-01  435197  31.2     M
42 2013-02-01  424196  51.2     J
43 2013-03-01  473674  72.2     J
44 2013-04-01  619433  70.6     J
45 2013-05-01  663050 108.1     J
46 2013-06-01  761590 114.5     J
47 2013-07-01  823519 116.4     J
48 2013-08-01  843254 133.8     J
49 2013-09-01  937486 139.2     J
50 2013-10-01  987576 130.5     J
51 2013-11-01 1011535 160.5     J
52 2013-12-01 1018478 133.8     J
53 2014-01-01 1086710 149.7     J
54 2014-02-01 1112480 162.3     J
55 2014-03-01 1178882 148.9     J
56 2014-04-01 1314470 112.2     J
57 2014-05-01 1336433 101.6     J
58 2014-06-01 1430031  87.8     J
59 2014-07-01 1521889  84.8     J
60 2014-08-01 1512314  79.3     J
> 

参考文献
・石田基広.『改訂2版 R言語逆引きハンドブック』.C&R研究所.pp703

アプリケーション
R Core Team (2013). R: A language and environment for statistical computing.
R Foundation for Statistical Computing, Vienna, Austria.
URL http://www.R-project.org/.