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):



    • Related Articles

    • recode()

      The recode() function can be used to rename any of the values of a variable. Example: # recode the variable `Year` and save it is a new column in the data frame Fingers$Year_recode <- recode(Fingers$Year, "1" = "First", "2" = "Second", "3" = "Third", ...
    • Using the R Sandbox

      The R Sandbox The R Sandbox is a separate R window you can use to experiment with R code and play around with ideas from the course. It comes pre-loaded with the base R packages and all of the packages and datasets we reference in the textbook. ...
    • Statistical Model

      A statistical model is a simplified way of describing how data are generated. It helps us separate what we can explain using known information from what we cannot explain perfectly. We use statistical models for three main purposes: (1) to understand ...
    • R Function

      An R function is a reusable block of code in the R programming language that performs a specific task. Functions accept inputs (called arguments), execute a set of instructions, and return an output. They are fundamental to writing efficient, ...
    • Data Generating Process (DGP)

      Data Generating Process (DGP) The Data Generating Process (DGP) refers to the underlying mechanism—real or hypothetical—that produces the data we observe. A DGP specifies how variables are related, how randomness enters the system, and how observed ...