do() provides a natural syntax for repetition tuned to assist
with replication and resampling methods.
Usage
do(object, ...)
# S3 method for class 'numeric'
do(object, ...)
# Default S3 method
do(object, ...)
Do(n = 1L, cull = NULL, mode = "default", algorithm = 1, parallel = TRUE)
# S3 method for class 'repeater'
print(x, ...)
# S4 method for class 'repeater,ANY'
e1 * e2Arguments
- 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, andmatrix.- 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 namedmean.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))).
If you like the name result, try wrapping the expression in { }:
do(3) * { mean(~ height, data = resample(Galton)) }.
Author
Daniel Kaplan ([email protected]) and Randall Pruim ([email protected])
Examples
do(3) * rnorm(1)
#> rnorm
#> 1 0.9537124
#> 2 -0.3084539
#> 3 -0.1936198
do(3) * "hello"
#> hello
#> 1 hello
#> 2 hello
#> 3 hello
do(3) * 1:4
#> V1 V2 V3 V4
#> 1 1 2 3 4
#> 2 1 2 3 4
#> 3 1 2 3 4
do(3) * mean(rnorm(25))
#> mean
#> 1 -0.32228041
#> 2 0.13824416
#> 3 0.04960635
do(3) * lm(shuffle(height) ~ sex + mother, Galton)
#> Intercept sexM mother sigma r.squared F numdf dendf
#> 1 72.74839 -0.1303453 -0.09238138 3.580112 0.0037920883 1.703419 2 895
#> 2 64.77094 -0.1727158 0.03244444 3.585030 0.0010531400 0.471777 2 895
#> 3 65.71198 -0.1644362 0.01769318 3.585709 0.0006749052 0.302224 2 895
#> .row .index
#> 1 1 1
#> 2 1 2
#> 3 1 3
do(3) * anova(lm(shuffle(height) ~ sex + mother, Galton))
#> source df SS MS F pval .row
#> sex...1 sex 1 1.200378e+01 12.0037764 0.93463244 0.3339232 1
#> mother...2 mother 1 8.294034e+00 8.2940336 0.64578618 0.4218366 2
#> Residuals...3 Residuals 895 1.149476e+04 12.8433124 NA NA 3
#> sex...4 sex 1 1.623471e+00 1.6234711 0.12621332 0.7224747 1
#> mother...5 mother 1 1.130675e+00 1.1306750 0.08790193 0.7669303 2
#> Residuals...6 Residuals 895 1.151231e+04 12.8629142 NA NA 3
#> sex...7 sex 1 1.758369e-01 0.1758369 0.01366807 0.9069572 1
#> mother...8 mother 1 8.987272e-01 0.8987272 0.06985945 0.7916024 2
#> Residuals...9 Residuals 895 1.151399e+04 12.8647908 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)))
#> sample.mean
#> 1 -0.07759286
#> 2 0.22312550
#> 3 0.04760878
# change the names on the fly
do(3) * mean(~height, data = resample(Galton))
#> mean
#> 1 66.61247
#> 2 66.69477
#> 3 66.88731
do(3) * c(mean_height = mean(~height, data = resample(Galton)))
#> mean_height
#> 1 66.80846
#> 2 66.41080
#> 3 66.75022
# this results in the name being `result`
do(3) * { mean(~height, data = resample(Galton)) }
#> result
#> 1 66.65891
#> 2 66.68875
#> 3 66.68641
set.rseed(1234)
do(3) * tally( ~sex|treat, data=resample(HELPrct))
#> female.no male.no female.yes male.yes
#> 1 45 184 59 165
#> 2 52 179 58 164
#> 3 52 163 43 195
set.rseed(1234) # re-using seed gives same results again
do(3) * tally( ~sex|treat, data=resample(HELPrct))
#> female.no male.no female.yes male.yes
#> 1 45 184 59 165
#> 2 52 179 58 164
#> 3 52 163 43 195