我在R中有一个列表,每个元素都有可变数量的字符串,例如:
我想把这个列表转换成一个2列的矩阵,如果它们出现在列表的同一个元素中,就把两个字符串并排放在一起。例如:
这个怎么办呢?
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"
这个怎么办呢?
unlist
并输入矩阵中会稍微快一些 -matrix(unlist(lapply(el, combn, 2)), ncol=2, byrow=TRUE)
- 不过我还没有测试过... - thelatemaillst
吗? - adm,
拆分数据框列,即do.call(rbind, lapply(strsplit(as.character(data$Col), ","), function(x) t(combn(x, 2))))
。 - akrunif/else
条件,即if(length(x) > 2) do this else x
。 - akrun