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

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.

主站蜘蛛池模板: 璧山县| 深州市| 余江县| 古丈县| 收藏| 巴彦县| 东山县| 忻城县| 鹿泉市| 阿瓦提县| 莱州市| 信丰县| 杨浦区| 东丰县| 商都县| 迁西县| 纳雍县| 光山县| 新蔡县| 丰台区| 玉林市| 徐闻县| 高青县| 杭锦后旗| 清苑县| 桐柏县| 庄河市| 伊吾县| 永福县| 交城县| 宾阳县| 油尖旺区| 分宜县| 昌宁县| 鸡泽县| 长兴县| 锦州市| 普宁市| 和田县| 孟村| 南靖县|