- Hands-On Geospatial Analysis with R and QGIS
- Shammunul Islam
- 366字
- 2021-06-10 18:44:23
Matrix
Suppose, we also have the prices of these three items for the month of June as follows:
june_price = c(20, 25, 33)
Now if we want to stack all these three months in a single variable, we can't use vectors anymore; we need a new data structure. One of the data structures to rescue in this case is the matrix. A matrix is basically a two-dimensional array of data elements with a number of rows and columns fixed. Like a vector, a matrix can also contain just one type of element; a mix of two types is not allowed. To combine these three vectors with every row corresponding to a particular month's prices of different items and every column corresponding to prices of different items in a particular month, what we can do is first combine these three vectors inside a matrix() command, followed by a comma and nrow = 3, indicating the fact that there are three different items (for example, items are arranged row-wise and months are arranged column-wise).
all_prices = matrix(c(jan_price, mar_price, june_price), nrow= 3)
all_prices
The all_prices data frame will look like the following:
[,1] [,2] [,3]
[1,] 10 11 20
[2,] 20 22 25
[3,] 30 33 33
Now suppose we change our mind and want to arrange this with the items displayed column-wise and the prices displayed row-wise; that is, the first row corresponds to the prices of different items in a particular month and the first column corresponds to the first month's (January's) prices of different items, with that arrangement continuing for every other row and column. We can do so very easily by mentioning byrow = TRUE inside the matrix. byrow = TRUE arranges the values of vectors row-wise. It arranges the matrix by aligning all the elements row-wise allowing for its dimensions:
all_prices2 = matrix(c(jan_price, mar_price, june_price), nrow= 3, byrow = TRUE)
all_prices2
The output will look like the following:
[,1] [,2] [,3]
[1,] 10 20 30
[2,] 11 22 33
[3,] 20 25 33
We can see that here jan_price is considered as the first row, mar_price as the second row, and june_price as the third row in all_prices2.
- 虛擬儀器設(shè)計(jì)測(cè)控應(yīng)用典型實(shí)例
- 高效能辦公必修課:Word圖文處理
- 集成架構(gòu)中型系統(tǒng)
- Word 2000、Excel 2000、PowerPoint 2000上機(jī)指導(dǎo)與練習(xí)
- 輕松學(xué)PHP
- 快學(xué)Flash動(dòng)畫(huà)百例
- 21天學(xué)通C++
- Python Data Science Essentials
- SharePoint 2010開(kāi)發(fā)最佳實(shí)踐
- 基于多目標(biāo)決策的數(shù)據(jù)挖掘方法評(píng)估與應(yīng)用
- 21天學(xué)通Java Web開(kāi)發(fā)
- Python:Data Analytics and Visualization
- Docker on Amazon Web Services
- 網(wǎng)絡(luò)服務(wù)器搭建與管理
- Building Google Cloud Platform Solutions