有没有办法遍历 R 中的矩阵/df 来创建邻接矩阵? [英] Is there a way to loop through a matrix/df in R to create an adjacency matrix?
问题描述
我正在尝试遍历 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)
我想遍历每一行并更新一个新的矩阵/df,其中 [search] 中每个数字的计数将是输出.
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
) 或者如果您是按行或列运行,考虑两个边距的嵌套 sapply
和 apply
解决方案:
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屋!