【问题标题】:Getting all splits of numeric sequence in R在R中获取数字序列的所有拆分
【发布时间】:2018-05-27 11:59:51
【问题描述】:

我正在尝试在 R 中获取序列 [1:n] 的所有可能拆分,例如:

getSplits(0,3)

应该返回序列 123 的所有可能拆分,换句话说(在向量列表中):

[1] 1
[2] 1 2
[3] 1 2 3
[4] 1 3
[5] 2
[6] 2 3
[7] 3

现在我创建了一个函数,它可以递归地访问这些向量,但无法将它们组合成一个,如上所述。我的功能是:

getSplits <- function(currentDigit, lastDigit, split) {
  splits=list();
  for (nextDigit in currentDigit: lastDigit)
  {
    currentSplit <- c(split, c(nextDigit));
    print(currentSplit);
    if(nextDigit < lastDigit) {
      possibleSplits = c(list(currentSplit), getSplits(nextDigit+1, lastDigit, currentSplit));
    }else{
      possibleSplits = currentSplit;
    }
    splits <- c(splits, list(possibleSplits));
  }
  return(splits);
} 

打印每个 currentSplit 会产生我需要的所有正确向量,但最终的返回列表(拆分)以某种方式将它们嵌套到更深层次的列表中,返回:

[1] 1

[[1]][[2]]
[[1]][[2]][[1]]
[1] 1 2

[[1]][[2]][[2]]
[1] 1 2 3


[[1]][[3]]
[1] 1 3


[[2]]
[[2]][[1]]
[1] 2

[[2]][[2]]
[1] 2 3


[[3]]
[1] 3

对于对应的函数调用getSplits(1, 3, c())

如果有人能帮助我按照我上面描述的方式工作,将不胜感激!

【问题讨论】:

  • 代码中不需要分号。此外,如果 splits 已经是一个列表,那么您可能只需要c(splits, possibleSplits)。最后,您可以使用combn 作为生成此函数的函数,对1:3 进行lapply 处理。我现在太忙了,但是 SO 上存在类似的问题。

标签: r list recursion vector sequence


【解决方案1】:

字符向量输出

试试combn:

k <- 3
s <- unlist(lapply(1:k, combn, x = k, toString))
s
## [1] "1"       "2"       "3"       "1, 2"    "1, 3"    "2, 3"    "1, 2, 3"

数据帧输出

如果您希望输出采用数据框的形式:

read.table(text = s, header = FALSE, sep = ",", fill = TRUE, col.names = 1:k)

给予:

  X1 X2 X3
1  1 NA NA
2  2 NA NA
3  3 NA NA
4  1  2 NA
5  1  3 NA
6  2  3 NA
7  1  2  3

列出输出

或列表:

lapply(s, function(x) scan(textConnection(x), quiet = TRUE, sep = ","))

给予:

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] 1 2

[[5]]
[1] 1 3

[[6]]
[1] 2 3

[[7]]
[1] 1 2 3

更新:纳入了 cmets 中提到的改进以及进一步的简化,还添加了数据框和列表输出。

【讨论】:

  • 我觉得可以简写成unlist(lapply(1:k, function(m) combn(k, m, FUN = toString)))
【解决方案2】:

这是另一种方法:

f <- function(nums) sapply(1:length(nums), function(x) t(combn(nums, m = x)))
f(1:3)

这会产生

[[1]]
     [,1]
[1,]    1
[2,]    2
[3,]    3

[[2]]
     [,1] [,2]
[1,]    1    2
[2,]    1    3
[3,]    2    3

[[3]]
     [,1] [,2] [,3]
[1,]    1    2    3

【讨论】:

    【解决方案3】:

    OP 正在寻找c(1,2,3)Power set。有几个软件包可以让您快速完成这一操作。使用包rje,我们有:

    library(rje)
    powerSet(c(1,2,3))
    [[1]]
    numeric(0)
    
    [[2]]
    [1] 1
    
    [[3]]
    [1] 2
    
    [[4]]
    [1] 1 2
    
    [[5]]
    [1] 3
    
    [[6]]
    [1] 1 3
    
    [[7]]
    [1] 2 3
    
    [[8]]
    [1] 1 2 3
    

    ...和iterpc:

    library(iterpc)
    getall(iterpc(c(2,1,1,1), 3, labels = 0:3))
         [,1] [,2] [,3]
    [1,]    0    0    1
    [2,]    0    0    2
    [3,]    0    0    3
    [4,]    0    1    2
    [5,]    0    1    3
    [6,]    0    2    3
    [7,]    1    2    3
    

    更一般地说,

    n <- 3
    getall(iterpc(c(n-1,rep(1, n)), n, labels = 0:n)) ## same as above
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2022-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-08
      相关资源
      最近更新 更多