日付型データへの変換

> dataset <- read.table("clipboard" , header=TRUE)

> dataset
        date CoreCPI
1   2013/1/1    -0.2
2   2013/2/1    -0.3
3   2013/3/1    -0.5
4   2013/4/1    -0.4
5   2013/5/1     0.0
6   2013/6/1     0.4
7   2013/7/1     0.7
8   2013/8/1     0.8
9   2013/9/1     0.7
10 2013/10/1     0.9
11 2013/11/1     1.2
12 2013/12/1     1.3
13  2014/1/1     1.3
14  2014/2/1     1.3
15  2014/3/1     1.3
16  2014/4/1     3.2
17  2014/5/1     3.4
18  2014/6/1     3.3
19  2014/7/1     3.3

> x<-dataset$date

> y<-dataset$CoreCPI

> x
 [1] 2013/1/1  2013/2/1  2013/3/1  2013/4/1  2013/5/1  2013/6/1  2013/7/1  2013/8/1  2013/9/1  2013/10/1
[11] 2013/11/1 2013/12/1 2014/1/1  2014/2/1  2014/3/1  2014/4/1  2014/5/1  2014/6/1  2014/7/1 
19 Levels: 2013/1/1 2013/10/1 2013/11/1 2013/12/1 2013/2/1 2013/3/1 2013/4/1 2013/5/1 2013/6/1 ... 2014/7/1

> y
 [1] -0.2 -0.3 -0.5 -0.4  0.0  0.4  0.7  0.8  0.7  0.9  1.2  1.3  1.3  1.3  1.3  3.2  3.4  3.3  3.3

 > x<-as.Date(x)

 > x
 [1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01"
 [9] "2013-09-01" "2013-10-01" "2013-11-01" "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01"
[17] "2014-05-01" "2014-06-01" "2014-07-01"

> x[19]-x[16]
Time difference of 91 days

> mean(x)
[1] "2013-09-30"

> diff(x)
Time differences in days
 [1] 31 28 31 30 31 30 31 31 30 31 30 31 31 28 31 30 31 30

 > x[-1]
 [1] "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01" "2013-09-01"
 [9] "2013-10-01" "2013-11-01" "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01"
[17] "2014-06-01" "2014-07-01"

> x[-19]
 [1] "2013-01-01" "2013-02-01" "2013-03-01" "2013-04-01" "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01"
 [9] "2013-09-01" "2013-10-01" "2013-11-01" "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01"
[17] "2014-05-01" "2014-06-01"

> x[1:3]
[1] "2013-01-01" "2013-02-01" "2013-03-01"

> x[-(1:3)]
 [1] "2013-04-01" "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01" "2013-11-01"
 [9] "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01" "2014-07-01"
  
> x[ c(1,3)]
[1] "2013-01-01" "2013-03-01"

> x[-c(1,3)]
 [1] "2013-02-01" "2013-04-01" "2013-05-01" "2013-06-01" "2013-07-01" "2013-08-01" "2013-09-01" "2013-10-01"
 [9] "2013-11-01" "2013-12-01" "2014-01-01" "2014-02-01" "2014-03-01" "2014-04-01" "2014-05-01" "2014-06-01"
[17] "2014-07-01"

> x[20]
[1] NA

> x[0]
character(0)

> as.Date("2014/8/3")
[1] "2014-08-03"

> as.Date(2014/8/3)
 以下にエラー as.Date.numeric(2014/8/3) : 
   'origin' を指定しなければなりません 
>