Skip to contents

Provides an alternative to the dplyr:do() using nest(), mutate() and map() to apply a function to a grouped data frame.

Usage

doo(.data, .fun, ..., unnest = TRUE)

Arguments

.data

a (grouped) data frame

.fun

A function, formula, or atomic vector.

...

Additional arguments passed on to .fun

unnest

Logical argument defaults to TRUE to control if results of .fun should be unnested. Valid only if the result is of class data.frame or tbl_df.

Value

a data frame

Details

If the applied function returns a data frame, then the output will be automatically unnested. Otherwise, the output includes the grouping variables and a column named "data" , which is a "list-columns" containing the results for group combinations.

Author

Tiago Olivoto tiagoolivoto@gmail.com

Examples

# \donttest{
library(metan)
# Head the first two lines of each environment
data_ge2 %>%
 group_by(ENV) %>%
 doo(~head(., 2))
#> # A tibble: 8 × 18
#>   ENV   GEN   REP      PH    EH    EP    EL    ED    CL    CD    CW    KW    NR
#>   <fct> <fct> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 A1    H1    1      2.61  1.71 0.658  16.1  52.2  28.1  16.3  25.1  217.  15.6
#> 2 A1    H1    2      2.87  1.76 0.628  14.2  50.3  27.6  14.5  21.4  184.  16  
#> 3 A2    H1    1      3.00  1.88 0.626  15.1  50.8  31.1  15.6  30.8  191.  16.4
#> 4 A2    H1    2      2.97  1.83 0.617  15.2  52.1  31.2  15.7  30.9  197.  17.2
#> 5 A3    H1    1      2.11  1.05 0.497  15.7  49.9  30.5  16.6  28.6  164.  15.6
#> 6 A3    H1    2      2.20  1.09 0.492  13.7  49.2  30.5  14.7  22.3  130.  16.4
#> 7 A4    H1    1      2.38  1.28 0.537  14.1  49.8  29.1  14.5  23.8  165.  17.2
#> 8 A4    H1    2      2.72  1.52 0.558  16.9  53.3  31.7  17.9  32.2  226.  18  
#> # … with 5 more variables: NKR <dbl>, CDED <dbl>, PERK <dbl>, TKW <dbl>,
#> #   NKE <dbl>

# Genotype analysis for each environment using 'gafem()'
# variable PH
data_ge2 %>%
  group_by(ENV) %>%
  doo(~gafem(., GEN, REP, PH, verbose = FALSE))
#> # A tibble: 4 × 2
#>   ENV   data   
#>   <fct> <list> 
#> 1 A1    <gafem>
#> 2 A2    <gafem>
#> 3 A3    <gafem>
#> 4 A4    <gafem>
# }