我必须在r中把一个向量分成n个相等大小的块,我找不到任何基函数来做这个。谷歌也没帮上什么忙。这是我目前想到的;

x <- 1:10
n <- 3
chunk <- function(x,n) split(x, factor(sort(rank(x)%%n)))
chunk(x,n)
$`0`
[1] 1 2 3

$`1`
[1] 4 5 6 7

$`2`
[1]  8  9 10

当前回答

还有一种可能是package parallel中的splitIndices函数:

library(parallel)
splitIndices(20, 3)

给:

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

[[2]]
[1]  8  9 10 11 12 13

[[3]]
[1] 14 15 16 17 18 19 20

注意:这只适用于数值。如果你想拆分一个字符向量,你需要做一些索引:lapply(splitIndices(20,3), \(x) letters[1:20][x])

其他回答

这是另一种变体。

注意:在这个示例中,您在第二个参数中指定CHUNK SIZE

所有的块都是均匀的,除了最后一块; 最后一个最坏的情况是更小,永远不会比块大小大。

chunk <- function(x,n)
{
    f <- sort(rep(1:(trunc(length(x)/n)+1),n))[1:length(x)]
    return(split(x,f))
}

#Test
n<-c(1,2,3,4,5,6,7,8,9,10,11)

c<-chunk(n,5)

q<-lapply(c, function(r) cat(r,sep=",",collapse="|") )
#output
1,2,3,4,5,|6,7,8,9,10,|11,|

分割(x,矩阵(1:n, n,长度(x))(1:长度(x)))

也许这更清楚,但同样的想法: 分割(x,代表(1:n,天花板(长度(x) / n)、长度。Out = length(x))

如果你想对它排序,就对它进行排序

试试ggplot2函数,cut_number:

library(ggplot2)
x <- 1:10
n <- 3
cut_number(x, n) # labels = FALSE if you just want an integer result
#>  [1] [1,4]  [1,4]  [1,4]  [1,4]  (4,7]  (4,7]  (4,7]  (7,10] (7,10] (7,10]
#> Levels: [1,4] (4,7] (7,10]

# if you want it split into a list:
split(x, cut_number(x, n))
#> $`[1,4]`
#> [1] 1 2 3 4
#> 
#> $`(4,7]`
#> [1] 5 6 7
#> 
#> $`(7,10]`
#> [1]  8  9 10

它分成大小为⌊n/k⌋+1或⌊n/k⌋的块,并且不使用O(n log n)排序。

get_chunk_id<-function(n, k){
    r <- n %% k
    s <- n %/% k
    i<-seq_len(n)
    1 + ifelse (i <= r * (s+1), (i-1) %/% (s+1), r + ((i - r * (s+1)-1) %/% s))
}

split(1:10, get_chunk_id(10,3))

你可以像mdsummer建议的那样,结合split/cut和quantile来创建偶数组:

split(x,cut(x,quantile(x,(0:n)/n), include.lowest=TRUE, labels=FALSE))

这为您的示例提供了相同的结果,但不适用于倾斜变量。