How to Use the repmat() Function in R


Often you may want to create a matrix in R by replicating values several times in a specific manner.

One of the easiest ways to do so is by using the repmat() function from the pracma package in R, which can be used to perform this exact task.

The repmat() function uses the following syntax:

repmat(a, n, m = n)

where:

  • a: Name of vector or matrix to be replicated
  • n, m: Number of times to replicate in each dimension.

The following examples show how to use the repmat() function in practice in several different scenarios in R.

Note: Before using the repmat() function, you may need to first install the pracma package. You can use the following syntax to do so:

install.packages('pracma')

Once the pracma package has been installed, you can proceed to use the repmat() function.

Example 1: Use repmat() to Create Square Matrix with One Value

Suppose that we would like to create a square matrix in R in which every value in the matrix contains the same number.

We can use the repmat() function to easily perform this task.

For example, we can use the following syntax to create a matrix with 5 columns and 5 rows in which each value in the matrix is equal to 1:

#create 5x5 matrix in which each value is equal to 1
repmat(1, 5)

     [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    1    1    1
[2,]    1    1    1    1    1
[3,]    1    1    1    1    1
[4,]    1    1    1    1    1
[5,]    1    1    1    1    1

Feel free to replace the first argument in the repmat() function to instead populate the matrix with a different value.

For example, we could use the following syntax to instead create a matrix with 5 columns and 5 rows in which each value in the matrix is equal to 10:

#create 5x5 matrix in which each value is equal to 10
repmat(10, 5)

     [,1] [,2] [,3] [,4] [,5]
[1,]   10   10   10   10   10
[2,]   10   10   10   10   10
[3,]   10   10   10   10   10
[4,]   10   10   10   10   10
[5,]   10   10   10   10   10

Example 2: Use repmat() to Create Square Matrix with Random Values

Suppose that we would like to create a square matrix in R in which every value in the matrix contains a random number within a range.

We can use the repmat() function combined with the runif() function to accomplish this.

For example, we can use the following syntax to create a matrix with 5 columns and 5 rows in which each value in the matrix is equal the same random number between 0 and 10:

#create 5x5 matrix in which each value is same random number
repmat(runif(1, min=0, max=10), 5)

         [,1]     [,2]     [,3]     [,4]     [,5]
[1,] 2.459905 2.459905 2.459905 2.459905 2.459905
[2,] 2.459905 2.459905 2.459905 2.459905 2.459905
[3,] 2.459905 2.459905 2.459905 2.459905 2.459905
[4,] 2.459905 2.459905 2.459905 2.459905 2.459905
[5,] 2.459905 2.459905 2.459905 2.459905 2.459905

This returns a 5×5 matrix in which each value inside the matrix is 2.450095, a random number that we generated using the runif() function to generate a random value from the uniform distribution with a range of 0 to 10.

Example 3: Use repmat() to Create Matrix with Repeated Values

Suppose that we would like to create a matrix in R in which each row contains the same pattern of repeated values.

We can use the repmat() function with the following syntax to do so:

#create matrix by repeating 1, 2 exactly 5 times
repmat(c(1, 2), 5)

     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,]    1    2    1    2    1    2    1    2    1     2
[2,]    1    2    1    2    1    2    1    2    1     2
[3,]    1    2    1    2    1    2    1    2    1     2
[4,]    1    2    1    2    1    2    1    2    1     2
[5,]    1    2    1    2    1    2    1    2    1     2

Feel free to replace the first argument in the repmat() function with any vector of values that you would like to be repeated.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Create a Frequency Table by Group in R
How to Create a Frequency Polygon in R
How to Create Relative Frequency Tables in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *