R

时间:2016-09-25 13:59:25

标签: r sorting

我正在为特定输入制作所有可能的组合,但必须根据输入的顺序进行排序。由于组合的大小不同,我正在努力解决之前发布的答案。

我想知道这是否可能。

输入:

D N A 3

这意味着我需要以最多3个字符串的所有组合输出它:

D
DD
DDD
DDN
DDA
DND
DNA
. 
.

如果我们考虑D<N<A

,这基本上是升序

到目前为止,我的输出看起来像这样:

A
AA
AAA
AAD
AAN
AD
ADA
ADD
ADN
AN
.
.

我已尝试将输入转换为因子c("D","N","A")并对输出进行排序,但随后它会消失任何大于1个字符的字符串。

2 个答案:

答案 0 :(得分:3)

这是一个可能的解决方案:

generateCombs <- function(x, n){
  if (n == 1) return(x[1]) # Base case
  # Create a grid with all possible permutations of 0:n. 0 == "", and 1:n correspond to elements of x
  permutations = expand.grid(replicate(n, 0:n, simplify = F)) 
  # Order permutations
  orderedPermutations = permutations[do.call(order, as.list(permutations)),] 
  # Map permutations now such that 0 == "", and 1:n correspond to elements of x
    mappedPermutations = sapply(orderedPermutations, function(y) c("", x)[y + 1])
  # Collapse each row into a single string
  collapsedPermutations = apply(mappedPermutations, 1, function(x) paste0(x, collapse = ""))
  # Due to the 0's, there will be duplicates. We remove the duplicates in reverse order
  collapsedPermutations = rev(unique(rev(collapsedPermutations)))[-1] # -1 removes blank
  # Return as data frame
  return (as.data.frame(collapsedPermutations))
}

x = c("D", "N", "A")
n = 3
generateCombs(x, n)

输出结果为:

   collapsedPermutations
1                      D
2                     DD
3                    DDD
4                    DDN
5                    DDA
6                     DN
7                    DND
8                    DNN
9                    DNA
10                    DA
11                   DAD
...

答案 1 :(得分:1)

使用我刚刚找到的随机库(因此我可能使用它错误)的解决方案称为iterpc

生成所有组合,对元素进行排序,排序,然后入侵字符串。

ordered_combn = function(elems) {
  require(data.table)
  require(iterpc)

  I = lapply(seq_along(elems), function(i) iterpc::iterpc(table(elems), i, replace=TRUE, ordered=TRUE))
  I = lapply(I, iterpc::getall)
  I = lapply(I, as.data.table)

  dt = rbindlist(I, fill = TRUE)
  dt[is.na(dt)] = ""

  cols = paste0("V", 1:length(elems))
  dt[, (cols) := lapply(.SD, factor, levels = c("", elems)), .SDcols = cols]

  setkey(dt)
  dt[, ID := 1:.N]
  dt[, (cols) := lapply(.SD, as.character), .SDcols = cols]
  dt[, ord := paste0(.SD, collapse = ""), ID, .SDcols = cols]

  # return dt[, ord] as an ordered factor for neatness
  dt
}

elems = c("D", "N", "A")
combs = ordered_combn(elems)
combs

输出

    V1 V2 V3 ID ord
 1:  D        1   D
 2:  D  D     2  DD
 3:  D  D  D  3 DDD
 4:  D  D  N  4 DDN
 5:  D  D  A  5 DDA
 6:  D  N     6  DN
 7:  D  N  D  7 DND
 8:  D  N  N  8 DNN
...