Here we show how to use the rep()
function for
replication and the seq()
function for sequence generation
in R.
To replicate a vector or a
factor in R, we can use the rep()
function from the "base"
package.
Using the "vec" vector below.
To replicate it two times:
[1] 1 2 3 1 2 3
[1] 1 2 3 1 2 3
To replicate each element two times:
[1] 1 1 2 2 3 3
To replicate the vector until we have 8 elements:
[1] 1 2 3 1 2 3 1 2
To replicate each element two times and the resulting vector two times:
[1] 1 1 2 2 3 3 1 1 2 2 3 3
To replicate each element two times until we have 8 elements:
[1] 1 1 2 2 3 3 1 1
To generate a sequence in R, we can use the seq()
function from the "base" package.
For a sequence from 1 to 5:
[1] 1 2 3 4 5
[1] 1 2 3 4 5
We can set the difference or gap between each element with the "by" argument:
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
[1] 1.0 2.5 4.0
We can set the number of elements in the sequence to 9:
[1] 1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0
We can set the number of elements in the sequence to the length of another vector:
[1] 1 3 5
The feedback form is a Google form but it does not collect any personal information.
Please click on the link below to go to the Google form.
Thank You!
Go to Feedback Form
Copyright © 2020 - 2024. All Rights Reserved by Stats Codes