2019年12月5日木曜日

xts , matrix and data frame index and dimensions.


# the sample of xts object.

> cli_xts["2019::"]
               oecd      usa    china     ea19
2019-01-01 99.53469 99.71027 98.70411 99.77903
2019-02-01 99.43623 99.54673 98.69403 99.66678
2019-03-01 99.36298 99.40987 98.71894 99.55812
2019-04-01 99.30513 99.28092 98.75597 99.45299
2019-05-01 99.25108 99.15568 98.80080 99.34872
2019-06-01 99.20237 99.03459 98.86234 99.24314
2019-07-01 99.15969 98.91864 98.94160 99.14515
2019-08-01 99.12648 98.81747 99.02364 99.06464
2019-09-01 99.10692 98.75613 99.10512 99.00034

# in order to create the sample matirx, use matrix() to transform xts to marix

> matrix(cli_xts["2019::"],ncol=4)
          [,1]     [,2]     [,3]     [,4]
 [1,] 99.53469 99.71027 98.70411 99.77903
 [2,] 99.43623 99.54673 98.69403 99.66678
 [3,] 99.36298 99.40987 98.71894 99.55812
 [4,] 99.30513 99.28092 98.75597 99.45299
 [5,] 99.25108 99.15568 98.80080 99.34872
 [6,] 99.20237 99.03459 98.86234 99.24314
 [7,] 99.15969 98.91864 98.94160 99.14515
 [8,] 99.12648 98.81747 99.02364 99.06464
 [9,] 99.10692 98.75613 99.10512 99.00034

# matrix require 2 dim. index like [1,] to pick up the designated row.

> matrix(cli_xts["2019::"],ncol=4)[1,]
[1] 99.53469 99.71027 98.70411 99.77903

# while xts requires [1].

> cli_xts["2019::"][1]
               oecd      usa    china     ea19
2019-01-01 99.53469 99.71027 98.70411 99.77903

> cli_xts["2019::"][1,1]
               oecd
2019-01-01 99.53469

> matrix(cli_xts["2019::"],ncol=4)[1,1]
[1] 99.53469

# for the case of data frame.

> data.frame(cli_xts["2019::"],ncol=4)
               oecd      usa    china     ea19 ncol
2019-01-01 99.53469 99.71027 98.70411 99.77903    4
2019-02-01 99.43623 99.54673 98.69403 99.66678    4
2019-03-01 99.36298 99.40987 98.71894 99.55812    4
2019-04-01 99.30513 99.28092 98.75597 99.45299    4
2019-05-01 99.25108 99.15568 98.80080 99.34872    4
2019-06-01 99.20237 99.03459 98.86234 99.24314    4
2019-07-01 99.15969 98.91864 98.94160 99.14515    4
2019-08-01 99.12648 98.81747 99.02364 99.06464    4
2019-09-01 99.10692 98.75613 99.10512 99.00034    4

# a index works in the same way as matrix.

> data.frame(cli_xts["2019::"],ncol=4)[1,]
               oecd      usa    china     ea19 ncol
2019-01-01 99.53469 99.71027 98.70411 99.77903    4


> data.frame(cli_xts["2019::"],ncol=4)[1,1]
[1] 99.53469

# a single dimention index also works fine for data frame
# but it takes column for the parameter

> data.frame(cli_xts["2019::"],ncol=4)[1]
               oecd
2019-01-01 99.53469
2019-02-01 99.43623
2019-03-01 99.36298
2019-04-01 99.30513
2019-05-01 99.25108
2019-06-01 99.20237
2019-07-01 99.15969
2019-08-01 99.12648
2019-09-01 99.10692

# on the other hand, single dim. index taken for the row.

> cli_xts["2019::"][1]
               oecd      usa    china     ea19
2019-01-01 99.53469 99.71027 98.70411 99.77903

0 件のコメント: