官术网_书友最值得收藏!

Array

Arrays are also like matrices, but they allow us to have more than two dimensions. The all_prices2 row has prices of different items for January, March, and June 2018. Now, suppose we also want to record prices for 2017. We can do so by using array() and in this case we want to add two 3x3 matrices where the first one corresponds to 2018 and the latter matrix corresponds to 2017. In array(m, n, p), m and n stand for the dimensions of the matrix and p stands for how many matrices we want to store.

In the following example, we define six vectors for three different months for two different years. Now we create an array by combining six different vectors using c() and by using them inside array() as inputs as follows:

# Create six vectors
jan_2018 = c(10, 11, 20)
mar_2018 = c(20, 22, 25)
june_2018 = c(30, 33, 33)
jan_2017 = c(10, 10, 17)
mar_2017 = c(18, 23, 21)
june_2017 = c(25, 31, 35)
# Now combine these vectors into array
combined = array(c(jan_2018, mar_2018, june_2018, jan_2017, mar_2017, june_2017),dim = c(3,3,2))
combined

We can now see that we have two matrices of 3 x 3 dimensions, as in the output as follows:

主站蜘蛛池模板: 仙桃市| 芜湖县| 鲁山县| 新干县| 三河市| 新郑市| 香格里拉县| 本溪市| 芜湖市| 沙田区| 固镇县| 阳城县| 克拉玛依市| 绥芬河市| 宾阳县| 东至县| 雷山县| 聂荣县| 同心县| 高青县| 杨浦区| 盐边县| 大姚县| 大余县| 南通市| 古田县| 辽宁省| 鹤岗市| 湄潭县| 邢台县| 萨嘎县| 富顺县| 博野县| 亚东县| 濮阳县| 兴国县| 巴彦县| 建昌县| 万宁市| 文山县| 那坡县|