有没有办法在R中的矩阵/df中循环以创建邻接矩阵? [英] Is there a way to loop through a matrix/df in R to create an adjacency matrix?

查看:87
本文介绍了有没有办法在R中的矩阵/df中循环以创建邻接矩阵?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图遍历data.frame中的53行,并使用结果创建一个邻接矩阵.但是,由于循环无法正确运行,我的努力继续停滞.

I am trying to loop through 53 rows in a data.frame and create an adjacency matrix with the results. However, my efforts continue to be stalled by the fact that the loop will not run correctly.

我尝试创建匹配并应用许多count()函数,但均未成功.

I have tried to create matches as well as applying numerous count() functions, without success.

MRE :(实际上,数据要大得多,所以我唯一的搜索实际上是217k个元素)

MRE: (In truth, the data is a lot larger so my unique search is actually 217k elements)

df1<-data.frame(col1=c(12345,123456,1234567,12345678),
col2=c(54321,54432,12345,76543),
col3=c(11234,12234,1234567,123345),
col4=c(54321,54432,12345,76543))

search<-c(12345,1234567,75643,54432)

我想遍历每一行并更新一个新的matrix/df,其中[搜索]中每个数字的计数将作为输出.

I would like to loop through each row and update a new matrix/df where the count per number in [search] would be the output.

例如:

df2

        12345     1234567    75643    54432
row1    TRUE       TRUE      FALSE    FALSE
row2    FALSE      FALSE     TRUE      TRUE
row3    TRUE       TRUE      FALSE    FALSE
row4    TRUE       FALSE     TRUE     TRUE

推荐答案

虽然由于计数甚至可能出现错字(75643 != 76543),或者您是按行还是按列运行,但不清楚如何得出计数值,请考虑使用嵌套两个边距的sapplyapply解决方案:

While it is unclear how your counts are derived as there might even be a typo (75643 != 76543) or if you are running by rows or columns, consider a nested sapply and apply solution for both margins:

按行

search <- c(12345, 1234567, 76543, 54432)                                # ADJUSTED TYPO    
mat <- sapply(search, function(s) apply(df1, 1, function(x) s %in% x))   # 1 FOR ROW MARGIN

colnames(mat) <- search
rownames(mat) <- paste0("row", seq(nrow(df1)))

mat
#      12345 1234567 76543 54432
# row1  TRUE   FALSE FALSE FALSE
# row2 FALSE   FALSE FALSE  TRUE
# row3  TRUE    TRUE FALSE FALSE
# row4 FALSE   FALSE  TRUE FALSE

按列

search <- c(12345, 1234567, 76543, 54432)                                # ADJUSTED TYPO
mat <- sapply(search, function(s) apply(df1, 2, function(x) s %in% x))   # 2 FOR COL MARGIN

colnames(mat) <- search
rownames(mat) <- paste0("col", seq(ncol(df1)))

mat
#      12345 1234567 76543 54432
# col1  TRUE    TRUE FALSE FALSE
# col2  TRUE   FALSE  TRUE  TRUE
# col3 FALSE    TRUE FALSE FALSE
# col4  TRUE   FALSE  TRUE  TRUE

妊娠期演示

这篇关于有没有办法在R中的矩阵/df中循环以创建邻接矩阵?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆