Use to split dataframes for parallel processing

chunk_df(.gdf, ..., .nchunks = parallel::detectCores(), .as_list = TRUE)

Arguments

.gdf

(grouped) dataframe

...

grouping variables, either a character vector or NSE-style column names

.nchunks

set number of chunks

.as_list

return dataframe as a list

Examples

# Chunking will split the rows of the dataframe as evenly
# as possible into the number chunks specified.

library(dplyr)
Theoph %>% chunk_df(.nchunks = 6)
#> $`1`
#>    Subject   Wt Dose  Time  conc chunk__
#> 1        1 79.6 4.02  0.00  0.74       1
#> 2        1 79.6 4.02  0.25  2.84       1
#> 3        1 79.6 4.02  0.57  6.57       1
#> 4        1 79.6 4.02  1.12 10.50       1
#> 5        1 79.6 4.02  2.02  9.66       1
#> 6        1 79.6 4.02  3.82  8.58       1
#> 7        1 79.6 4.02  5.10  8.36       1
#> 8        1 79.6 4.02  7.03  7.47       1
#> 9        1 79.6 4.02  9.05  6.89       1
#> 10       1 79.6 4.02 12.12  5.94       1
#> 11       1 79.6 4.02 24.37  3.28       1
#> 12       2 72.4 4.40  0.00  0.00       1
#> 13       2 72.4 4.40  0.27  1.72       1
#> 14       2 72.4 4.40  0.52  7.91       1
#> 15       2 72.4 4.40  1.00  8.31       1
#> 16       2 72.4 4.40  1.92  8.33       1
#> 17       2 72.4 4.40  3.50  6.85       1
#> 18       2 72.4 4.40  5.02  6.08       1
#> 19       2 72.4 4.40  7.03  5.40       1
#> 20       2 72.4 4.40  9.00  4.55       1
#> 21       2 72.4 4.40 12.00  3.01       1
#> 22       2 72.4 4.40 24.30  0.90       1
#> 
#> $`2`
#>    Subject   Wt Dose  Time conc chunk__
#> 23       3 70.5 4.53  0.00 0.00       2
#> 24       3 70.5 4.53  0.27 4.40       2
#> 25       3 70.5 4.53  0.58 6.90       2
#> 26       3 70.5 4.53  1.02 8.20       2
#> 27       3 70.5 4.53  2.02 7.80       2
#> 28       3 70.5 4.53  3.62 7.50       2
#> 29       3 70.5 4.53  5.08 6.20       2
#> 30       3 70.5 4.53  7.07 5.30       2
#> 31       3 70.5 4.53  9.00 4.90       2
#> 32       3 70.5 4.53 12.15 3.70       2
#> 33       3 70.5 4.53 24.17 1.05       2
#> 34       4 72.7 4.40  0.00 0.00       2
#> 35       4 72.7 4.40  0.35 1.89       2
#> 36       4 72.7 4.40  0.60 4.60       2
#> 37       4 72.7 4.40  1.07 8.60       2
#> 38       4 72.7 4.40  2.13 8.38       2
#> 39       4 72.7 4.40  3.50 7.54       2
#> 40       4 72.7 4.40  5.02 6.88       2
#> 41       4 72.7 4.40  7.02 5.78       2
#> 42       4 72.7 4.40  9.02 5.33       2
#> 43       4 72.7 4.40 11.98 4.19       2
#> 44       4 72.7 4.40 24.65 1.15       2
#> 
#> $`3`
#>    Subject   Wt Dose  Time  conc chunk__
#> 45       5 54.6 5.86  0.00  0.00       3
#> 46       5 54.6 5.86  0.30  2.02       3
#> 47       5 54.6 5.86  0.52  5.63       3
#> 48       5 54.6 5.86  1.00 11.40       3
#> 49       5 54.6 5.86  2.02  9.33       3
#> 50       5 54.6 5.86  3.50  8.74       3
#> 51       5 54.6 5.86  5.02  7.56       3
#> 52       5 54.6 5.86  7.02  7.09       3
#> 53       5 54.6 5.86  9.10  5.90       3
#> 54       5 54.6 5.86 12.00  4.37       3
#> 55       5 54.6 5.86 24.35  1.57       3
#> 56       6 80.0 4.00  0.00  0.00       3
#> 57       6 80.0 4.00  0.27  1.29       3
#> 58       6 80.0 4.00  0.58  3.08       3
#> 59       6 80.0 4.00  1.15  6.44       3
#> 60       6 80.0 4.00  2.03  6.32       3
#> 61       6 80.0 4.00  3.57  5.53       3
#> 62       6 80.0 4.00  5.00  4.94       3
#> 63       6 80.0 4.00  7.00  4.02       3
#> 64       6 80.0 4.00  9.22  3.46       3
#> 65       6 80.0 4.00 12.10  2.78       3
#> 66       6 80.0 4.00 23.85  0.92       3
#> 
#> $`4`
#>    Subject   Wt Dose  Time conc chunk__
#> 67       7 64.6 4.95  0.00 0.15       4
#> 68       7 64.6 4.95  0.25 0.85       4
#> 69       7 64.6 4.95  0.50 2.35       4
#> 70       7 64.6 4.95  1.02 5.02       4
#> 71       7 64.6 4.95  2.02 6.58       4
#> 72       7 64.6 4.95  3.48 7.09       4
#> 73       7 64.6 4.95  5.00 6.66       4
#> 74       7 64.6 4.95  6.98 5.25       4
#> 75       7 64.6 4.95  9.00 4.39       4
#> 76       7 64.6 4.95 12.05 3.53       4
#> 77       7 64.6 4.95 24.22 1.15       4
#> 78       8 70.5 4.53  0.00 0.00       4
#> 79       8 70.5 4.53  0.25 3.05       4
#> 80       8 70.5 4.53  0.52 3.05       4
#> 81       8 70.5 4.53  0.98 7.31       4
#> 82       8 70.5 4.53  2.02 7.56       4
#> 83       8 70.5 4.53  3.53 6.59       4
#> 84       8 70.5 4.53  5.05 5.88       4
#> 85       8 70.5 4.53  7.15 4.73       4
#> 86       8 70.5 4.53  9.07 4.57       4
#> 87       8 70.5 4.53 12.10 3.00       4
#> 88       8 70.5 4.53 24.12 1.25       4
#> 
#> $`5`
#>     Subject   Wt Dose  Time  conc chunk__
#> 89        9 86.4  3.1  0.00  0.00       5
#> 90        9 86.4  3.1  0.30  7.37       5
#> 91        9 86.4  3.1  0.63  9.03       5
#> 92        9 86.4  3.1  1.05  7.14       5
#> 93        9 86.4  3.1  2.02  6.33       5
#> 94        9 86.4  3.1  3.53  5.66       5
#> 95        9 86.4  3.1  5.02  5.67       5
#> 96        9 86.4  3.1  7.17  4.24       5
#> 97        9 86.4  3.1  8.80  4.11       5
#> 98        9 86.4  3.1 11.60  3.16       5
#> 99        9 86.4  3.1 24.43  1.12       5
#> 100      10 58.2  5.5  0.00  0.24       5
#> 101      10 58.2  5.5  0.37  2.89       5
#> 102      10 58.2  5.5  0.77  5.22       5
#> 103      10 58.2  5.5  1.02  6.41       5
#> 104      10 58.2  5.5  2.05  7.83       5
#> 105      10 58.2  5.5  3.55 10.21       5
#> 106      10 58.2  5.5  5.05  9.18       5
#> 107      10 58.2  5.5  7.08  8.02       5
#> 108      10 58.2  5.5  9.38  7.14       5
#> 109      10 58.2  5.5 12.10  5.68       5
#> 110      10 58.2  5.5 23.70  2.42       5
#> 
#> $`6`
#>     Subject   Wt Dose  Time conc chunk__
#> 111      11 65.0 4.92  0.00 0.00       6
#> 112      11 65.0 4.92  0.25 4.86       6
#> 113      11 65.0 4.92  0.50 7.24       6
#> 114      11 65.0 4.92  0.98 8.00       6
#> 115      11 65.0 4.92  1.98 6.81       6
#> 116      11 65.0 4.92  3.60 5.87       6
#> 117      11 65.0 4.92  5.02 5.22       6
#> 118      11 65.0 4.92  7.03 4.45       6
#> 119      11 65.0 4.92  9.03 3.62       6
#> 120      11 65.0 4.92 12.12 2.69       6
#> 121      11 65.0 4.92 24.08 0.86       6
#> 122      12 60.5 5.30  0.00 0.00       6
#> 123      12 60.5 5.30  0.25 1.25       6
#> 124      12 60.5 5.30  0.50 3.96       6
#> 125      12 60.5 5.30  1.00 7.82       6
#> 126      12 60.5 5.30  2.00 9.72       6
#> 127      12 60.5 5.30  3.52 9.75       6
#> 128      12 60.5 5.30  5.07 8.57       6
#> 129      12 60.5 5.30  7.07 6.59       6
#> 130      12 60.5 5.30  9.03 6.11       6
#> 131      12 60.5 5.30 12.05 4.57       6
#> 132      12 60.5 5.30 24.15 1.17       6
#> 

# Chunking with grouped data will evenly distribute the 
# number of unique group elements across the chunks.  

Theoph %>% group_by(Subject) %>% chunk_df(.nchunks = 5)
#> $`1`
#> # A tibble: 33 × 6
#> # Groups:   Subject [3]
#>    Subject    Wt  Dose  Time  conc chunk__
#>    <ord>   <dbl> <dbl> <dbl> <dbl>   <int>
#>  1 1        79.6  4.02  0     0.74       1
#>  2 1        79.6  4.02  0.25  2.84       1
#>  3 1        79.6  4.02  0.57  6.57       1
#>  4 1        79.6  4.02  1.12 10.5        1
#>  5 1        79.6  4.02  2.02  9.66       1
#>  6 1        79.6  4.02  3.82  8.58       1
#>  7 1        79.6  4.02  5.1   8.36       1
#>  8 1        79.6  4.02  7.03  7.47       1
#>  9 1        79.6  4.02  9.05  6.89       1
#> 10 1        79.6  4.02 12.1   5.94       1
#> # ℹ 23 more rows
#> 
#> $`2`
#> # A tibble: 33 × 6
#> # Groups:   Subject [3]
#>    Subject    Wt  Dose  Time  conc chunk__
#>    <ord>   <dbl> <dbl> <dbl> <dbl>   <int>
#>  1 4        72.7   4.4  0     0          2
#>  2 4        72.7   4.4  0.35  1.89       2
#>  3 4        72.7   4.4  0.6   4.6        2
#>  4 4        72.7   4.4  1.07  8.6        2
#>  5 4        72.7   4.4  2.13  8.38       2
#>  6 4        72.7   4.4  3.5   7.54       2
#>  7 4        72.7   4.4  5.02  6.88       2
#>  8 4        72.7   4.4  7.02  5.78       2
#>  9 4        72.7   4.4  9.02  5.33       2
#> 10 4        72.7   4.4 12.0   4.19       2
#> # ℹ 23 more rows
#> 
#> $`3`
#> # A tibble: 22 × 6
#> # Groups:   Subject [2]
#>    Subject    Wt  Dose  Time  conc chunk__
#>    <ord>   <dbl> <dbl> <dbl> <dbl>   <int>
#>  1 7        64.6  4.95  0     0.15       3
#>  2 7        64.6  4.95  0.25  0.85       3
#>  3 7        64.6  4.95  0.5   2.35       3
#>  4 7        64.6  4.95  1.02  5.02       3
#>  5 7        64.6  4.95  2.02  6.58       3
#>  6 7        64.6  4.95  3.48  7.09       3
#>  7 7        64.6  4.95  5     6.66       3
#>  8 7        64.6  4.95  6.98  5.25       3
#>  9 7        64.6  4.95  9     4.39       3
#> 10 7        64.6  4.95 12.0   3.53       3
#> # ℹ 12 more rows
#> 
#> $`4`
#> # A tibble: 22 × 6
#> # Groups:   Subject [2]
#>    Subject    Wt  Dose  Time  conc chunk__
#>    <ord>   <dbl> <dbl> <dbl> <dbl>   <int>
#>  1 9        86.4   3.1  0     0          4
#>  2 9        86.4   3.1  0.3   7.37       4
#>  3 9        86.4   3.1  0.63  9.03       4
#>  4 9        86.4   3.1  1.05  7.14       4
#>  5 9        86.4   3.1  2.02  6.33       4
#>  6 9        86.4   3.1  3.53  5.66       4
#>  7 9        86.4   3.1  5.02  5.67       4
#>  8 9        86.4   3.1  7.17  4.24       4
#>  9 9        86.4   3.1  8.8   4.11       4
#> 10 9        86.4   3.1 11.6   3.16       4
#> # ℹ 12 more rows
#> 
#> $`5`
#> # A tibble: 22 × 6
#> # Groups:   Subject [2]
#>    Subject    Wt  Dose  Time  conc chunk__
#>    <ord>   <dbl> <dbl> <dbl> <dbl>   <int>
#>  1 11         65  4.92  0     0          5
#>  2 11         65  4.92  0.25  4.86       5
#>  3 11         65  4.92  0.5   7.24       5
#>  4 11         65  4.92  0.98  8          5
#>  5 11         65  4.92  1.98  6.81       5
#>  6 11         65  4.92  3.6   5.87       5
#>  7 11         65  4.92  5.02  5.22       5
#>  8 11         65  4.92  7.03  4.45       5
#>  9 11         65  4.92  9.03  3.62       5
#> 10 11         65  4.92 12.1   2.69       5
#> # ℹ 12 more rows
#> 

# The dataframe can be retained by preventing the output
# from being a list.

Theoph %>% chunk_df(Subject, .nchunks = 3, .as_list = FALSE)
#> # A tibble: 132 × 6
#> # Groups:   Subject [12]
#>    Subject    Wt  Dose  Time  conc chunk__
#>    <ord>   <dbl> <dbl> <dbl> <dbl>   <int>
#>  1 1        79.6  4.02  0     0.74       1
#>  2 1        79.6  4.02  0.25  2.84       1
#>  3 1        79.6  4.02  0.57  6.57       1
#>  4 1        79.6  4.02  1.12 10.5        1
#>  5 1        79.6  4.02  2.02  9.66       1
#>  6 1        79.6  4.02  3.82  8.58       1
#>  7 1        79.6  4.02  5.1   8.36       1
#>  8 1        79.6  4.02  7.03  7.47       1
#>  9 1        79.6  4.02  9.05  6.89       1
#> 10 1        79.6  4.02 12.1   5.94       1
#> # ℹ 122 more rows