The “rep” Function in R

  • Package: Base R (no specific package required)

  • Purpose: Replicates values to create a vector or array.

  • General Class: Data Transformation

  • Required Argument(s):

    • x: A vector or value to be replicated.

    • times: An integer specifying the number of times to replicate each element in x.

  • Notable Optional Arguments:

    • each: An integer indicating how many consecutive times each element of x should be repeated before the next element is taken.

    • length.out: An integer indicating the desired length of the result.

  • Example:

  • # Example data for using the rep function
    values <- c(1, 2, 3)

    # Replicate each value three times
    replicated_values <- rep(values, times = 3)

    # Display the result
    print(replicated_values)

  • In this example, the rep function is used to replicate each element in the values vector three times, resulting in a new vector (replicated_values). The times argument specifies the number of replications. The rep function is useful for creating repeated patterns or expanding vectors.

Previous
Previous

The “seq” Function in R

Next
Next

The “which” Function in R