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

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.

主站蜘蛛池模板: 轮台县| 桓台县| 绥德县| 黄山市| 濉溪县| 乳源| 岗巴县| 合江县| 怀宁县| 大冶市| 内黄县| 绥德县| 诸暨市| 城固县| 姜堰市| 会泽县| 鹤壁市| 峨眉山市| 灌南县| 乌苏市| 车致| 长海县| 清镇市| 本溪市| 仪陇县| 襄樊市| 兴城市| 和顺县| 江达县| 宕昌县| 平利县| 汉中市| 云林县| 鹤壁市| 五华县| 南投县| 德清县| 盈江县| 青河县| 敖汉旗| 东丰县|