Apply functions to grouped data frames
do.RdApplies functions on the subsets within a grouped data frame.
Usage
do(.data, ..., .frame = NULL, .env = parent.frame())
do_(data, x, frame = NULL, env = parent.frame())Arguments
- .data, data
A grouped data frame.
- ...
Expressions to be evaluated. Use
.to refer to the current group data frame. If named, results will be stored in a list column. If unnamed, and the expression returns a data frame, results will be inserted as is. Otherwise a list column will be created with a default column name.- .frame, frame
A character vector of column names referred to in
x. This subsets the data frame to only specified columns before evaluation. This provides safer evaluation by preventing accidental use of spurious global variables in place of column names or accidental use of spurious column names in place of global variables.- .env, env
An environment to look for objects outside those in
data.- x
A character vector (or list of character vectors) of expressions to apply to each group. Use
.to refer to the current group data frame. If named, results will be stored in a list column. If unnamed, and the expression returns a data frame, results will be inserted as is. Otherwise a list column will be created with a default column name.
Examples
#----------------------------------------------------------------------------
# do() examples
#----------------------------------------------------------------------------
library(bkdat)
df <- group_by(mtcars, "cyl")
do_(df, c(hp_t_test = "t.test(.$hp)"))
#> cyl
#> 1 4
#> 2 6
#> 3 8
#> hp_t_test
#> 1 13.091949639762, 10, 1.28276493881648e-07, 68.5723550302019, 96.7003722425253, 82.6363636363636, 0, 6.31199828216466, two.sided, One Sample t-test, .$hp
#> 2 13.3359867909848, 6, 1.09979354020709e-05, 99.8485022367553, 144.722926334673, 122.285714285714, 0, 9.16960373478926, two.sided, One Sample t-test, .$hp
#> 3 15.3561397406463, 13, 1.03317290728974e-09, 179.78110830284, 238.647463125731, 209.214285714286, 0, 13.6241457324405, two.sided, One Sample t-test, .$hp
do(df, hp_t_test = t.test(.$hp))
#> cyl
#> 1 4
#> 2 6
#> 3 8
#> hp_t_test
#> 1 13.091949639762, 10, 1.28276493881648e-07, 68.5723550302019, 96.7003722425253, 82.6363636363636, 0, 6.31199828216466, two.sided, One Sample t-test, .$hp
#> 2 13.3359867909848, 6, 1.09979354020709e-05, 99.8485022367553, 144.722926334673, 122.285714285714, 0, 9.16960373478926, two.sided, One Sample t-test, .$hp
#> 3 15.3561397406463, 13, 1.03317290728974e-09, 179.78110830284, 238.647463125731, 209.214285714286, 0, 13.6241457324405, two.sided, One Sample t-test, .$hp