Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

I just want to have an optimal script and in that there are 3 parameters. These parameters are: n = long of looping, x = any first number, y = any second number. The following are some examples:

The Result

1 Answer

0 votes
by (107k points)

If you want to perform that you can simply use the rep() to repeat x and y, n times.

x <- 13

y <- 10

n <- 2

rep(c(x, y), n)

#[1] 13 10 13 10

If you want to use for loop, you can also do that, refer the following :

vector <- integer(2 * n)

for (i in seq_len(n)) {

  vector[c(2 * i -1, 2 * i)] <- c(x, y)

}

vector

#[1] 13 10 13 10

If you are a beginner and want to know more R, then do check out the R programming tutorial that will help you in learning R from scratch.

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...