# Split to chunks of size n — chunks_of_n • stRoke
Split to chunks of size n

Skip to contents

# Split to chunks of size n

Source: `R/chunks_of_n.R`
`chunks_of_n.Rd`

Split to chunks of size n

## Usage

    chunks_of_n(d, n, label = NULL, even = FALSE, pattern = NULL)

## Arguments

d

  data. Can be vector or data frame.

n

  number of chunks

label

  naming prefix for chunk names

even

  boolean to set if size of chunks should be evenly distributed.

pattern

  regex pattern to extract names from provided vector. If data frame, will assume first column is name.

## Value

List of length n

## Examples

    tail(chunks_of_n(seq_len(100),7),3)
    #> $`13`
    #> [1] 85 86 87 88 89 90 91
    #>
    #> $`14`
    #> [1] 92 93 94 95 96 97 98
    #>
    #> $`15`
    #> [1]  99 100
    #>
    tail(chunks_of_n(seq_len(100),7,even=TRUE),3)
    #> $`13`
    #> [1] 83 84 85 86 87 88
    #>
    #> $`14`
    #> [1] 89 90 91 92 93 94
    #>
    #> $`15`
    #> [1]  95  96  97  98  99 100
    #>
    ds <- data.frame(nm=paste0("Sub",
    add_padding(rownames(stRoke::talos))),stRoke::talos)
    head(chunks_of_n(ds,7,pattern="Sub[0-9]{3}",label="grp"),2)
    #> $`grp-Sub038-Sub011`
    #>         nm  rtreat mrs_1 mrs_6 hypertension diabetes   civil
    #> 38  Sub038  Active     1     1           no       no partner
    #> 434 Sub434  Active     1     1          yes       no partner
    #> 588 Sub588  Active     2     2          yes       no partner
    #> 42  Sub042  Active     0     0          yes       no partner
    #> 160 Sub160 Placebo     1     1          yes       no partner
    #> 174 Sub174  Active     0     1          yes       no   alone
    #> 11  Sub011 Placebo     2     1          yes      yes   alone
    #>
    #> $`grp-Sub601-Sub062`
    #>         nm  rtreat mrs_1 mrs_6 hypertension diabetes   civil
    #> 601 Sub601 Placebo     1     1           no       no partner
    #> 412 Sub412  Active     0     0          yes       no partner
    #> 88  Sub088 Placebo     1     1          yes      yes partner
    #> 56  Sub056 Placebo     0     0           no       no   alone
    #> 235 Sub235 Placebo     2     1          yes      yes   alone
    #> 205 Sub205  Active     3     3           no       no partner
    #> 62  Sub062  Active     2     2          yes       no   alone
    #>
    ## Please notice that no sorting is performed. This is on purpose to preserve
    ## original sorting. If sorting is intended, try something like this:
    ds[order(ds$nm),] |> chunks_of_n(7,pattern="Sub[0-9]{3}",label="grp") |>
    head(2)
    #> $`grp-Sub001-Sub020`
    #>        nm  rtreat mrs_1 mrs_6 hypertension diabetes   civil
    #> 1  Sub001 Placebo     0     0          yes       no partner
    #> 2  Sub002 Placebo     2     1           no       no partner
    #> 7  Sub007 Placebo     1     1          yes       no partner
    #> 9  Sub009 Placebo     1     1           no       no partner
    #> 11 Sub011 Placebo     2     1          yes      yes   alone
    #> 13 Sub013  Active     1     1          yes       no partner
    #> 20 Sub020 Placebo     1     2           no       no   alone
    #>
    #> $`grp-Sub022-Sub038`
    #>        nm  rtreat mrs_1 mrs_6 hypertension diabetes   civil
    #> 22 Sub022 Placebo     2     1           no       no partner
    #> 26 Sub026 Placebo     2     4           no       no   alone
    #> 28 Sub028  Active     2     2          yes       no partner
    #> 29 Sub029 Placebo     2     2           no       no   alone
    #> 33 Sub033 Placebo     0     0          yes       no   alone
    #> 34 Sub034  Active     1     1           no       no   alone
    #> 38 Sub038  Active     1     1           no       no partner
    #>