48.0k views
2 votes
Write a R code to Generate the following sequences using seq()or rep()function:

2,4,6,4,8,12,6,12,18,8,16,24

1 Answer

6 votes
Using the rep() function:
sequence <- rep(c(2, 4, 6), each = 3) * rep(c(1, 2, 3), times = 3)
This code first creates a repetition of the numbers 2, 4, and 6, each repeated three times using the rep() function. Then, it multiplies this sequence by a repetition of the numbers 1, 2, and 3, each repeated three times. The result is the desired sequence: 2, 4, 6, 4, 8, 12, 6, 12, 18, 8, 16, 24.
You can print the sequence variable to see the generated sequence in the R console.
Hope this helps :)
User Read Q
by
7.2k points