2016-06-15 8 views
3

に可変長リストを変換します例えば、私は、各要素は、文字列の可変数を持っているRでリストを持っているエッジリストIGRAPHのR

el: list 
chr [1:3] "sales", "environment", "communication" 
chr [1:2] "interpersonal", "microsoft office" 
chr [1:4] "writing", "reading", "excel", "python" 

私は2列の行列の中に、このリストを変換したいですそれらがリストの同じ要素に現れた場合、2つの文字列を並べて置きます。

matrix: 
"sales", "environment" 
"sales, "communication" 
"environment", "communication" 
"interpersonal", "microsoft office" 
"writing", "reading" 
"writing", "excel" 
"writing", "python" 
"reading", "excel" 
"reading", "python" 
"excel", "python" 

どうすればよいですか?

答えて

3

我々はmatrixで出力が必要な場合は、我々は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が述べたように、「LST」

をINGの unlistことにより、一度に複数回より tを呼び出すために迅速であるかもしれません
matrix(unlist(lapply(lst, combn, 2)), ncol=2, byrow=TRUE) 
+2

'unlist'と' matrix(unlist(lapply(el、combn、2))、ncol = 2、byrow = TRUE)の行列にフィードする方が少し速くなるかもしれません。 ' - それをテストしませんでした.. – thelatemail

+0

@thelatemailありがとう、私はそれを正しくチェックしませんでした。 – akrun

関連する問題