简体   繁体   English

如何基于2列数据帧创建1s和0s的矩阵

[英]How to create a matrix with 1s and 0s based off a 2-column data frame

Here is an example of what my data looks like (what I have is actually 1300 lines, or 1300 connections/edges between two different nodes): 以下是我的数据的示例(我拥有的实际是1300行,或两个不同节点之间的1300个连接/边):

node# node#
1  3
1  4
2  4
2  5
3  4
3  5

I currently have the above data in a data frame. 我目前在数据框中有上述数据。 This represent a network where a car can drive from node 1 to 3 or 1 to 4, and from node 2 to 4 or node 2 to 5, etc. I'd like to create a matrix that looks like this: 这代表一个网络,汽车可以从节点1驱动到3或1到4,从节点2到4或节点2到5等。我想创建一个如下所示的矩阵:

>      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    0    0    0    0    0    0
 [2,]    0    0    0    0    0    0
 [3,]    0    0    0    0    0    0
 [4,]    0    0    0    0    0    0
 [5,]    0    0    0    0    0    0

Where I'm stuck: I want to input 1s into the matrix from the leaving node, and a -1 in the matrix of the destination node, in the same column. 我被卡住的地方:我想在离开节点的矩阵中输入1,在同一列中输入目标节点矩阵中的-1。 So for this 6 node-connection data frame, the matrix would look like: 因此对于这个6节点连接数据帧,矩阵看起来像:

>      [,1] [,2] [,3] [,4] [,5] [,6]
 [1,]    1    1    0    0    0    0
 [2,]    0    0    1    1    0    0
 [3,]   -1    0    0    0    1    1
 [4,]    0   -1   -1    0   -1    0
 [5,]    0    0    0   -1    0   -1

But like I said, I have more than 1300 connections, so doing this by hand would take a while. 但就像我说的,我有超过1300个连接,所以用手做这个需要一段时间。 So I'm guessing matrix(0, 5, 1300) would be where I start? 所以我猜测矩阵(0,5,1300)将是我开始的地方?

You can index specific row/column pairs of a matrix using a 2-column indexing matrix. 您可以使用2列索引矩阵索引矩阵的特定行/列对。 This provides a convenient way to set all the 1's and then set all the -1's: 这提供了一种方便的方法来设置所有的1,然后设置所有-1的:

mat <- matrix(0, nrow=max(dat), ncol=nrow(dat))
mat[cbind(dat$node1, seq_len(nrow(dat)))] <- 1
mat[cbind(dat$node2, seq_len(nrow(dat)))] <- -1
mat
#      [,1] [,2] [,3] [,4] [,5] [,6]
# [1,]    1    1    0    0    0    0
# [2,]    0    0    1    1    0    0
# [3,]   -1    0    0    0    1    1
# [4,]    0   -1   -1    0   -1    0
# [5,]    0    0    0   -1    0   -1

(Thanks to @PierreLafortune for the trick about calling max on a data frame!) (感谢@PierreLafortune关于在数据帧上调用max的技巧!)

Data: 数据:

dat <- data.frame(node1=c(1, 1, 2, 2, 3, 3), node2=c(3, 4, 4, 5, 4, 5))

We could also use sparseMatrix from library(Matrix) 我们也可以使用library(Matrix) sparseMatrix library(Matrix)

library(Matrix)
B <- sparseMatrix(dat$node2, seq_len(nrow(dat)), x= -1)
mat <- sparseMatrix(dat$node1, seq_len(nrow(dat)), x= 1,
                      dims=dim(B)) + B
as.matrix(mat)
#      [,1] [,2] [,3] [,4] [,5] [,6]
#[1,]    1    1    0    0    0    0
#[2,]    0    0    1    1    0    0
#[3,]   -1    0    0    0    1    1
#[4,]    0   -1   -1    0   -1    0
#[5,]    0    0    0   -1    0   -1

NOTE: dat taken from @josliber's post. 注意: dat取自@ josliber的帖子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数据框的一列的值转换为 0 和 1? - How to convert the values of a column of a data frame to 0s and 1s? 根据多列中的字符串创建一个由 1 和 0 组成的矩阵 - Create a matrix of 1s and 0s based on strings in multiple columns 按日期(包括列标题)对数据框列进行分组,并汇总R中1和0的实例 - Group data frame columns by their dates (which comprise column titles) and summarize instances of 1s and 0s in R 如何从2个字符串向量创建1和0矩阵? - How do I create 1s and 0s matrix from 2 vectors of strings? 如何创建一个0和1的矩阵,使行和列的总和等于特定值? - How to create a matrix of 0s and 1s such that rows and columns sum up to particular value? 如何根据来自其他几个文件的值来创建R中的表1和0 - How to create a table in R with 1s and 0s based on the presence of values from several other files 在R中将一个数据帧转换为另一个具有0和1的数据帧 - convert a data frame to another data frame with 0s and 1s in R 在矩阵中将NA的矩阵转换为0并将连续变量转换为1 - converting a matrix of NAs to 0s and continous variables to 1s in a matrix 在r中的多列中计算0s 1s和2s - Counting 0s 1s and 2s in multiple column in r 如何在 R 中查找具有特定数量的连续 0 和 1 的矩阵中的行 - How to find rows in matrix with specific number of consecutive 0s and 1s in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM