do() provides a natural syntax for repetition tuned to assist with replication and resampling methods.

do(object, ...)

# S3 method for numeric
do(object, ...)

# S3 method for default
do(object, ...)

Do(n = 1L, cull = NULL, mode = "default", algorithm = 1, parallel = TRUE)

# S3 method for repeater
print(x, ...)

# S4 method for repeater,ANY
*(e1, e2)

Arguments

object

an object

...

additional arguments

n

number of times to repeat

cull

function for culling output of objects being repeated. If NULL, a default culling function is used. The default culling function is currently aware of objects of types lme, lm, htest, table, cointoss, and matrix.

mode

target mode for value returned

algorithm

a number used to select the algorithm used. Currently numbers below 1 use an older algorithm and numbers >=1 use a newer algorithm which is faster in some situations.

parallel

a logical indicating whether parallel computation should be attempted using the parallel package (if it is installed and loaded).

x

an object created by do.

e1

an object (in cases documented here, the result of running do)

e2

an object (in cases documented here, an expression to be repeated)

Value

do returns an object of class repeater which is only useful in the context of the operator *. See the examples.

Note

do is a thin wrapper around Do to avoid collision with dplyr::do() from the dplyr package.

Naming

The names used in the object returned from do() are inferred from the objects created in each replication. Roughly, this the strategy employed.

  • If the objects have names, those names are inherited, if possible.

  • If the objects do not have names, but do() is used with a simple function call, the name of that function is used. Example: do(3) * mean(~height, data = Galton) produces a data frame with a variable named mean.

  • In cases where names are not easily inferred and a single result is produced, it is named result.

To get different names, one can rename the objects as they are created, or rename the result returned from do(). Example of the former: do(3) * c(mean_height = mean(~height, data = resample(Galton))).

Author

Daniel Kaplan (kaplan@macalaster.edu) and Randall Pruim (rpruim@calvin.edu)

Examples

do(3) * rnorm(1)
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>        rnorm
#> 1 -0.7494472
#> 2  1.2921462
#> 3 -1.5571653
do(3) * "hello"
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>   hello
#> 1 hello
#> 2 hello
#> 3 hello
do(3) * 1:4
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>   V1 V2 V3 V4
#> 1  1  2  3  4
#> 2  1  2  3  4
#> 3  1  2  3  4
do(3) * mean(rnorm(25))
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>         mean
#> 1  0.0623133
#> 2  0.2401102
#> 3 -0.3108011
do(3) * lm(shuffle(height) ~ sex + mother, Galton)
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>   Intercept        sexM     mother    sigma    r.squared        F numdf dendf
#> 1  65.53609 -0.14171521 0.02025432 3.585880 0.0005795053 0.259479     2   895
#> 2  62.71526 -0.30775453 0.06561335 3.580173 0.0037583440 1.688204     2   895
#> 3  64.15243 -0.03260937 0.04096384 3.585619 0.0007249713 0.324660     2   895
#>   .row .index
#> 1    1      1
#> 2    1      2
#> 3    1      3
do(3) * anova(lm(shuffle(height) ~ sex + mother, Galton))
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>                  source  df           SS         MS          F      pval .row
#> sex...1             sex   1 2.687166e+01 26.8716589 2.09376158 0.1482517    1
#> mother...2       mother   1 1.622736e+00  1.6227361 0.12643888 0.7222371    2
#> Residuals...3 Residuals 895 1.148657e+04 12.8341542         NA        NA    3
#> sex...4             sex   1 2.950616e+00  2.9506165 0.22965706 0.6318953    1
#> mother...5       mother   1 1.321957e+01 13.2195671 1.02892629 0.3106851    2
#> Residuals...6 Residuals 895 1.149889e+04 12.8479242         NA        NA    3
#> sex...7             sex   1 8.103037e-01  0.8103037 0.06301493 0.8018503    1
#> mother...8       mother   1 5.522774e+00  5.5227745 0.42948991 0.5124078    2
#> Residuals...9 Residuals 895 1.150873e+04 12.8589154         NA        NA    3
#>               .index
#> sex...1            1
#> mother...2         1
#> Residuals...3      1
#> sex...4            2
#> mother...5         2
#> Residuals...6      2
#> sex...7            3
#> mother...8         3
#> Residuals...9      3
do(3) * c(sample.mean = mean(rnorm(25)))
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>   sample.mean
#> 1  -0.1857070
#> 2   0.2775839
#> 3   0.1669131
# change the names on the fly
do(3) * mean(~height, data = resample(Galton))
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>       mean
#> 1 66.63330
#> 2 66.76158
#> 3 66.75835
do(3) * c(mean_height = mean(~height, data = resample(Galton)))
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>   mean_height
#> 1    66.92940
#> 2    66.67238
#> 3    66.79822
set.rseed(1234)
do(3) * tally( ~sex|treat, data=resample(HELPrct))
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>   female.no male.no female.yes male.yes
#> 1        56     181         49      167
#> 2        53     162         58      180
#> 3        53     188         43      169
set.rseed(1234)  # re-using seed gives same results again
do(3) * tally( ~sex|treat, data=resample(HELPrct))
#> Using parallel package.
#>   * Set seed with set.rseed().
#>   * Disable this message with options(`mosaic:parallelMessage` = FALSE)
#>   female.no male.no female.yes male.yes
#> 1        56     181         49      167
#> 2        53     162         58      180
#> 3        53     188         43      169