Skip to contents

Create a data frame with a list-column.

Usage

nest_by(data, group_cols, nest_cols = NULL, nest_name = "data")

Arguments

data

A data frame.

group_cols

A character vector of variable names to group by (variables kept that won't be included in the list-column).

nest_cols

A character vector of variables names to nest (variables kept that will be included in the list-column). Defaults to every variable not being grouped on.

nest_name

A character string for the list-column name. Defaults to "data".

Value

nest_df

See also

Examples

#----------------------------------------------------------------------------
# nest_by() examples
#----------------------------------------------------------------------------
library(bkdat)

df <- nest_by(mtcars, group_cols = c("cyl", "carb"))
df
#>   cyl carb
#> 1   6    4
#> 2   4    1
#> 3   6    1
#> 4   8    2
#> 5   8    4
#> 6   4    2
#> 7   8    3
#> 8   6    6
#> 9   8    8
#>                                                                                                                                                                                                                                                                                                                                                                                                                           data
#> 1                                                                                                                                           21.000, 21.000, 19.200, 17.800, 160.000, 160.000, 167.600, 167.600, 110.000, 110.000, 123.000, 123.000, 3.900, 3.900, 3.920, 3.920, 2.620, 2.875, 3.440, 3.440, 16.460, 17.020, 18.300, 18.900, 0.000, 0.000, 1.000, 1.000, 1.000, 1.000, 0.000, 0.000, 4.000, 4.000, 4.000, 4.000
#> 2                                                                              22.800, 32.400, 33.900, 21.500, 27.300, 108.000, 78.700, 71.100, 120.100, 79.000, 93.000, 66.000, 65.000, 97.000, 66.000, 3.850, 4.080, 4.220, 3.700, 4.080, 2.320, 2.200, 1.835, 2.465, 1.935, 18.610, 19.470, 19.900, 20.010, 18.900, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 1.000, 0.000, 1.000, 4.000, 4.000, 4.000, 3.000, 4.000
#> 3                                                                                                                                                                                                                                                                                     21.400, 18.100, 258.000, 225.000, 110.000, 105.000, 3.080, 2.760, 3.215, 3.460, 19.440, 20.220, 1.000, 1.000, 0.000, 0.000, 3.000, 3.000
#> 4                                                                                                                                           18.700, 15.500, 15.200, 19.200, 360.000, 318.000, 304.000, 400.000, 175.000, 150.000, 150.000, 175.000, 3.150, 2.760, 3.150, 3.080, 3.440, 3.520, 3.435, 3.845, 17.020, 16.870, 17.300, 17.050, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 3.000, 3.000, 3.000, 3.000
#> 5 14.300, 10.400, 10.400, 14.700, 13.300, 15.800, 360.000, 472.000, 460.000, 440.000, 350.000, 351.000, 245.000, 205.000, 215.000, 230.000, 245.000, 264.000, 3.210, 2.930, 3.000, 3.230, 3.730, 4.220, 3.570, 5.250, 5.424, 5.345, 3.840, 3.170, 15.840, 17.980, 17.820, 17.420, 15.410, 14.500, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 0.000, 1.000, 3.000, 3.000, 3.000, 3.000, 3.000, 5.000
#> 6       24.400, 22.800, 30.400, 26.000, 30.400, 21.400, 146.700, 140.800, 75.700, 120.300, 95.100, 121.000, 62.000, 95.000, 52.000, 91.000, 113.000, 109.000, 3.690, 3.920, 4.930, 4.430, 3.770, 4.110, 3.190, 3.150, 1.615, 2.140, 1.513, 2.780, 20.000, 22.900, 18.520, 16.700, 16.900, 18.600, 1.000, 1.000, 1.000, 0.000, 1.000, 1.000, 0.000, 0.000, 1.000, 1.000, 1.000, 1.000, 4.000, 4.000, 4.000, 5.000, 5.000, 4.000
#> 7                                                                                                                                                                                                                                           16.40, 17.30, 15.20, 275.80, 275.80, 275.80, 180.00, 180.00, 180.00, 3.07, 3.07, 3.07, 4.07, 3.73, 3.78, 17.40, 17.60, 18.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 3.00, 3.00, 3.00
#> 8                                                                                                                                                                                                                                                                                                                                                                   19.70, 145.00, 175.00, 3.62, 2.77, 15.50, 0.00, 1.00, 5.00
#> 9                                                                                                                                                                                                                                                                                                                                                                   15.00, 301.00, 335.00, 3.54, 3.57, 14.60, 0.00, 1.00, 5.00