R language, seq and rep problem

In summary, the conversation discusses the use of rep() and seq() to create a specific vector. The attempt at a solution involves using seq() for the 'from' and 'to' arguments, but it is noted that rep() may also be needed. The solution is suggested to involve using a combination of seq() and rep() with a counter variable to create the desired vector.
  • #1
Mogarrr
120
6

Homework Statement


Using rep() and seq() as needed, create the vector
1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8 5 6 7 8 9

Homework Equations

The Attempt at a Solution


I figured out that seq() only takes numbers for the 'from' and 'to' arguments, since I tried seq(seq(...)).

Also, I'm not repeating the same sequence, so I'm not seeing how rep would work here. 1 and 9 are not repeated. 2,8 are appear twice. 3 and 7 are appear 3 times. And 4,5 are appear 4 times. rep(...,times) and rep(...,each) don't seem to work for this.

I can solve this using c(seq(1,5),...,seq(5,9)), although it seems the problem should be solved using only rep and seq.

I'm stumped here.
 
Physics news on Phys.org
  • #2
well you should try this. rep()
{
i = 1;
seq(i,i+4);
i++;
}
and check when i>5 then exit.
 

Related to R language, seq and rep problem

1. What is R language?

R language is a programming language and environment for statistical computing and graphics. It is widely used in data analysis and statistical modeling.

2. What is the "seq" function in R language?

The "seq" function in R language is used to generate a sequence of numbers. It takes in three arguments: starting value, ending value, and the interval between each value. For example, seq(1, 10, 2) will generate the sequence 1, 3, 5, 7, 9.

3. How can I use the "rep" function in R language?

The "rep" function in R language is used to replicate elements in a vector. It takes in two arguments: the element(s) to be repeated and the number of times to repeat. For example, rep(1, 3) will generate a vector with three 1s: 1, 1, 1.

4. What is the difference between "seq" and "rep" in R language?

The main difference between "seq" and "rep" in R language is that "seq" generates a sequence of numbers, while "rep" replicates elements in a vector. Additionally, "seq" takes in three arguments while "rep" takes in two arguments.

5. How can I solve a "seq and rep" problem in R language?

To solve a "seq and rep" problem in R language, you can use a combination of the "seq" and "rep" functions. First, use the "seq" function to generate the desired sequence of numbers. Then, use the "rep" function to replicate elements in the sequence as needed. You can also use loops and conditional statements to solve more complex problems involving "seq" and "rep" in R language.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
976
  • Nuclear Engineering
Replies
7
Views
2K
  • Precalculus Mathematics Homework Help
Replies
11
Views
756
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
945
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
Back
Top