do()*

do()*

The do()* function runs one or more lines of code the number of times specified inside the parentheses and returns the results as a data frame.

Example 1:

# take a random sample (n=10) of a variable, with replacement,

# and calculate the standard deviation of the 10 numbers, 3 times

do(3) * sd(resample(Fingers$Thumb, 10))

​
Example output (although actual values may vary due to random sampling variation):

Example 2:

# get the means of 10,000 random samples (n=157) with replacement

# and save in a new data frame
# use head() to view the first 6 rows

new_data <- do(10000) * mean(resample(Fingers$Thumb, 157)) 

head(new_data)

Example output (although actual values may vary due to random sampling variation):