R - 使用匹配在矩阵中写入单个值以在 R 中创建坐标向量
R - Writing individual values in a matrix using matching to create vector of coordinates in R
假设我创建了一个具有指定行数和列数的 0 矩阵:
r = 100
c = 100
zero_mat = matrix(rep(0, r*c),r,c)
我还有一个包含美国各州名称的向量,它有 100 行长,它看起来像:
states[1:7,1] #structured as a matrix with 1 column and many rows
California
Arizona
Wisconsin
California
Washington
Washington
Washington
那么,假设我只是将 zero_mat 的列名称设为州的名称:
colnames(zero_mat) = t(unique(states))
Now, here's the question. How do I write a 1 to elements in zero_map
corresponding to the row the state is in, in states
and the column
corresponding to the name of the state?
我确定这只是 1 行代码,但我花了几个小时试图弄清楚它无济于事。我认为这可能有效:zero_mat[1:r, states[1:r,1]] = 1
,但它只是 returns 1 的矩阵。
希望我理解正确。假设这是您的数据:
states = c('Ca', 'Ar', 'Wi', 'Ca', 'Wa', 'Wa', 'Wa')
而您的 zero_mat
及其列名定义如下:
states_uniq = unique(states)
zero_mat = matrix(0, ncol=length(states_uniq), nrow=length(states))
colnames(zero_mat) = states_uniq
## Ca Ar Wi Wa
## [1,] 0 0 0 0
## [2,] 0 0 0 0
## [3,] 0 0 0 0
## [4,] 0 0 0 0
## [5,] 0 0 0 0
## [6,] 0 0 0 0
## [7,] 0 0 0 0
您可以使用match
找到states
在states_uniq
中的位置
match(states, states_uniq)
## [1] 1 2 3 1 4 4 4
这些将是您要在 zero_mat
中设置的 1
的列索引。相应的行索引只是 1:length(states)
。因此,收集在 2 列矩阵的行中的 1
的行和列索引如下:
cbind(1:length(states), match(states, states_uniq))
## [,1] [,2]
## [1,] 1 1
## [2,] 2 2
## [3,] 3 3
## [4,] 4 1
## [5,] 5 4
## [6,] 6 4
## [7,] 7 4
此 2 列矩阵可用于索引 zero_mat
并将相应的条目设置为 1
:
zero_mat[ cbind(1:length(states), match(states, states_uniq)) ] = 1
## Ca Ar Wi Wa
## [1,] 1 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 1 0
## [4,] 1 0 0 0
## [5,] 0 0 0 1
## [6,] 0 0 0 1
## [7,] 0 0 0 1
如果数据集很大,您可能希望使用 Matrix
包中的稀疏矩阵来保存 space:
Matrix::sparseMatrix(i=1:length(states),
j=match(states, states_uniq),
x=1,
dimnames=list(NULL, states_uniq))
## 7 x 4 sparse Matrix of class "dgCMatrix"
## Ca Ar Wi Wa
## [1,] 1 . . .
## [2,] . 1 . .
## [3,] . . 1 .
## [4,] 1 . . .
## [5,] . . . 1
## [6,] . . . 1
## [7,] . . . 1
假设我创建了一个具有指定行数和列数的 0 矩阵:
r = 100
c = 100
zero_mat = matrix(rep(0, r*c),r,c)
我还有一个包含美国各州名称的向量,它有 100 行长,它看起来像:
states[1:7,1] #structured as a matrix with 1 column and many rows
California
Arizona
Wisconsin
California
Washington
Washington
Washington
那么,假设我只是将 zero_mat 的列名称设为州的名称:
colnames(zero_mat) = t(unique(states))
Now, here's the question. How do I write a 1 to elements in zero_map corresponding to the row the state is in, in
states
and the column corresponding to the name of the state?
我确定这只是 1 行代码,但我花了几个小时试图弄清楚它无济于事。我认为这可能有效:zero_mat[1:r, states[1:r,1]] = 1
,但它只是 returns 1 的矩阵。
希望我理解正确。假设这是您的数据:
states = c('Ca', 'Ar', 'Wi', 'Ca', 'Wa', 'Wa', 'Wa')
而您的 zero_mat
及其列名定义如下:
states_uniq = unique(states)
zero_mat = matrix(0, ncol=length(states_uniq), nrow=length(states))
colnames(zero_mat) = states_uniq
## Ca Ar Wi Wa
## [1,] 0 0 0 0
## [2,] 0 0 0 0
## [3,] 0 0 0 0
## [4,] 0 0 0 0
## [5,] 0 0 0 0
## [6,] 0 0 0 0
## [7,] 0 0 0 0
您可以使用match
找到states
在states_uniq
match(states, states_uniq)
## [1] 1 2 3 1 4 4 4
这些将是您要在 zero_mat
中设置的 1
的列索引。相应的行索引只是 1:length(states)
。因此,收集在 2 列矩阵的行中的 1
的行和列索引如下:
cbind(1:length(states), match(states, states_uniq))
## [,1] [,2]
## [1,] 1 1
## [2,] 2 2
## [3,] 3 3
## [4,] 4 1
## [5,] 5 4
## [6,] 6 4
## [7,] 7 4
此 2 列矩阵可用于索引 zero_mat
并将相应的条目设置为 1
:
zero_mat[ cbind(1:length(states), match(states, states_uniq)) ] = 1
## Ca Ar Wi Wa
## [1,] 1 0 0 0
## [2,] 0 1 0 0
## [3,] 0 0 1 0
## [4,] 1 0 0 0
## [5,] 0 0 0 1
## [6,] 0 0 0 1
## [7,] 0 0 0 1
如果数据集很大,您可能希望使用 Matrix
包中的稀疏矩阵来保存 space:
Matrix::sparseMatrix(i=1:length(states),
j=match(states, states_uniq),
x=1,
dimnames=list(NULL, states_uniq))
## 7 x 4 sparse Matrix of class "dgCMatrix"
## Ca Ar Wi Wa
## [1,] 1 . . .
## [2,] . 1 . .
## [3,] . . 1 .
## [4,] 1 . . .
## [5,] . . . 1
## [6,] . . . 1
## [7,] . . . 1