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

Editing a matrix in R

R allows us to edit (add, delete, or replace) elements of a matrix using the square bracket notation, as depicted in the following lines of code:

mat = matrix(c(1:10),nrow = 2, ncol = 5)
mat
mat[2,3]

How to do it…

In order to extract any element of a matrix, we can specify the position of that element in R using square brackets. For example, mat[2,3] will extract the element under the second row and the third column. The first numeric value corresponds to the row and the second numeric value corresponds to a column [row, column].

Similarly, to replace an element, we can type the following lines in R:

mat[2,3] = 16

To select all the elements of the second row, we can use mat[2, ]. If we do not specify any numeric value for a column, R will automatically assume all columns.

主站蜘蛛池模板: 温泉县| 和平县| 即墨市| 卫辉市| 安吉县| 林甸县| 陆丰市| 洛南县| 阿荣旗| 江陵县| 习水县| 深泽县| 新安县| 荆州市| 郯城县| 舟曲县| 关岭| 金华市| 江山市| 泰顺县| 义马市| 桂阳县| 茶陵县| 新邵县| 德清县| 隆尧县| 科尔| 通道| 淮阳县| 澎湖县| 庆阳市| 邵东县| 珲春市| 卢湾区| 土默特左旗| 孟州市| 铁岭市| 体育| 区。| 通城县| 广灵县|