将可变长度列表转换为边列表 igraph R

4
我在R中有一个列表,每个元素都有可变数量的字符串,例如:
el: list
chr [1:3] "sales", "environment", "communication"
chr [1:2] "interpersonal", "microsoft office"
chr [1:4] "writing", "reading", "excel", "python"

我想把这个列表转换成一个2列的矩阵,如果它们出现在列表的同一个元素中,就把两个字符串并排放在一起。例如:
matrix:
"sales", "environment"
"sales, "communication"
"environment", "communication"
"interpersonal", "microsoft office"
"writing", "reading"
"writing", "excel"
"writing", "python"
"reading", "excel"
"reading", "python"
"excel", "python"

这个怎么办呢?
1个回答

4

如果我们需要以矩阵形式输出,我们可以使用combn

do.call(rbind, lapply(lst, function(x) t(combn(x, 2))))
#     [,1]            [,2]              
# [1,] "sales"         "environment"     
# [2,] "sales"         "communication"   
# [3,] "environment"   "communication"   
# [4,] "interpersonal" "microsoft office"
# [5,] "writing"       "reading"         
# [6,] "writing"       "excel"           
# [7,] "writing"       "python"          
# [8,] "reading"       "excel"           
# [9,] "reading"       "python"          
#[10,] "excel"         "python"    

正如@thelatemail所提到的,调用t一次可能比通过unlist将“lst”多次调用更快。

matrix(unlist(lapply(lst, combn, 2)), ncol=2, byrow=TRUE)

2
好的答案。也许将其unlist并输入矩阵中会稍微快一些 - matrix(unlist(lapply(el, combn, 2)), ncol=2, byrow=TRUE) - 不过我还没有测试过... - thelatemail
我有同样的问题,但我的分隔符是分号(文本中有逗号),而且文本在数据框值中。也许我会开始另一个问题? - adm
列表的名称是什么?它是 lst 吗? - adm
@adm 也许您需要通过逗号,拆分数据框列,即do.call(rbind, lapply(strsplit(as.character(data$Col), ","), function(x) t(combn(x, 2)))) - akrun
1
@adm 因为你可能有一些只有一个单词的元素。在这种情况下,你需要一个 if/else 条件,即 if(length(x) > 2) do this else x - akrun
显示剩余5条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接