【问题标题】:Make a list of equal length vectors制作等长向量的列表
【发布时间】:2012-05-26 13:51:27
【问题描述】:

在 SO (LINK) 上的一个问题中,一位发帖人提出了一个问题,我给出了一个有效的答案,但有一部分让我感到困扰,从向量创建一个 list 以作为索引列表传递。所以说我有这个向量:

n <- 1:10
#> n
# [1]  1  2  3  4  5  6  7  8  9 10

假设我想将其分解为向量列表,每个向量的长度为 3。实现此目的的最佳(最短代码量和或最快)方法是什么?我们想丢弃第 10 项,因为它与 10/3 (length(n) - 10 %% 3) 有余数 1 (10 %% 3)。

这是想要的结果

list(1:3, 4:6, 7:9)

这将为我们提供那些不能组成三个一组的索引:

(length(n) + 1 - 10 %% 3):length(n)

编辑

这是 Wojciech Sobala 在 other thread 上发布的一个有趣的方法,链接到(我让他们在这里回答,如果他们回答,我会删除这个编辑)

n <- 100
l <- 3
n2 <- n - (n %% l)
split(1:n2, rep(1:n2, each=l, length=n2))

作为一个函数:

indices <- function(n, l){
    if(n > l) stop("n needs to be smaller than or equal to l")
    n2 <- n - (n %% l)
    cat("numbers", (n + 1 - n %% l):n, "did not make an index of length", l)
    split(1:n2, rep(1:n2, each=l, length=n2))
}

【问题讨论】:

  • 所有很好的答案,但我认为 X,He's 是最短的代码。谢谢,这让我很烦。所有的方法都比我正在做的更好。

标签: r


【解决方案1】:

这不是最短的,但这里有一个小递归版本:

wrap <- function(n,x,lx,y) {
    if (lx < n) return (y)
    z <- x[-(1:n)]
    wrap(n, z, length(z), c(y, list(x[1:n])))
}

wrapit <- function(x,n) {
    wrap(n,x,length(x),list())
}

> wrapit(1:10,3)
[[1]]
[1] 1 2 3

[[2]]
[1] 4 5 6

[[3]]
[1] 7 8 9

【讨论】:

  • 哇,这个伤了我的面条。 +1 我什至不确定它是如何工作的(尽管索引是相反的)
  • 要理解递归函数,有时用纸和铅笔跟踪函数调用链会有所帮助。或者在那里放几个打印语句来观察列表的增长和向量的缩小。
【解决方案2】:

不确定这是否有效?

x = function(x, n){ 
    if(n > x) stop("n needs to be smaller than or equal to x")
    output = matrix(1:(x-x%%n), ncol=(x-x%%n)/n, byrow=FALSE)
    output
}

编辑:将输出更改为列表

x = function(x, n){ 
    if(n > x) stop("n needs to be smaller than or equal to x")
    output = matrix(1:(x-x%%n), ncol=(x-x%%n)/n, byrow=TRUE)
    split(output, 1:nrow(output))
}

Example:
x(10, 3)
$`1`
[1] 1 2 3

$`2`
[1] 4 5 6

$`3`
[1] 7 8 9

【讨论】:

  • 这不是一个列表,所以除非你用data.frame包装output,否则它不会在该海报的问题中起作用
  • @TylerRinker:我将输出更改为列表。
【解决方案3】:
xx <- 1:10
xxr <- rle(0:(length(1:10)-1) %/% 3)  # creates an rle object
fac3 <- rep( xxr$values[xxr$lengths == 3], each=3)  #selects the one of length 3
                                     # and recreates the shortened grouping vector
tapply(xx[ 1:length(fac3)],          # a shortened original vector
                       fac3, list)   # split into little lists
$`0`                                # Hope you don't mind having names on your list
[1] 1 2 3

$`1`
[1] 4 5 6

$`2`
[1] 7 8 9

【讨论】:

  • 我实际上并没有编辑你的回复,只是把我的编辑放错了地方,抱歉。 +1
猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 2013-08-13
  • 1970-01-01
  • 2015-08-15
  • 2018-12-31
  • 2020-01-20
  • 2020-02-18
  • 1970-01-01
相关资源
最近更新 更多