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 -1.8056091
#> 2  0.1914100
#> 3  0.2423873
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.1142425314
#> 2 -0.0002201565
#> 3 -0.2875418309
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
#> 1  66.52871 -0.05339408  0.004051405 3.586805 6.370146e-05 0.02850822     2
#> 2  68.00014 -0.01440351 -0.019224550 3.586641 1.554971e-04 0.06959576     2
#> 3  63.54233  0.10569387  0.049366686 3.584777 1.194536e-03 0.53519426     2
#>   dendf .row .index
#> 1   895    1      1
#> 2   895    1      2
#> 3   895    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 1.758369e-01  0.1758369 0.01367136 0.9069460    1
#> mother...2       mother   1 3.666937e+00  3.6669368 0.28510519 0.5935070    2
#> Residuals...3 Residuals 895 1.151122e+04 12.8616979         NA        NA    3
#> sex...4             sex   1 8.396726e-01  0.8396726 0.06535430 0.7982838    1
#> mother...5       mother   1 1.525575e+01 15.2557506 1.18740204 0.2761475    2
#> Residuals...6 Residuals 895 1.149897e+04 12.8480078         NA        NA    3
#> sex...7             sex   1 1.491102e+01 14.9110212 1.16193197 0.2813557    1
#> mother...8       mother   1 1.465629e+01 14.6562916 1.14208233 0.2855009    2
#> Residuals...9 Residuals 895 1.148550e+04 12.8329554         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.01235415
#> 2  0.09427374
#> 3 -0.09801412
# 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.76381
#> 2 66.72617
#> 3 66.64187
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.73597
#> 2    66.67350
#> 3    66.57661
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