Learn R Programming

mosaic (version 1.9.1)

do: Do Things Repeatedly

Description

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

Usage

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)

Value

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

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)

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)

See Also

replicate(), set.rseed()

Examples

Run this code
do(3) * rnorm(1)
do(3) * "hello"
do(3) * 1:4
do(3) * mean(rnorm(25))
do(3) * lm(shuffle(height) ~ sex + mother, Galton)
do(3) * anova(lm(shuffle(height) ~ sex + mother, Galton))
do(3) * c(sample.mean = mean(rnorm(25)))
# change the names on the fly
do(3) * mean(~height, data = resample(Galton))
do(3) * c(mean_height = mean(~height, data = resample(Galton)))
set.rseed(1234)
do(3) * tally( ~sex|treat, data=resample(HELPrct))
set.rseed(1234)  # re-using seed gives same results again
do(3) * tally( ~sex|treat, data=resample(HELPrct))

Run the code above in your browser using DataLab