R language, seq and rep problem

Click For Summary
SUMMARY

The discussion focuses on generating a specific vector in R using the functions rep() and seq(). The desired output is 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. Participants noted that seq() requires numeric arguments for 'from' and 'to', and expressed challenges in using rep() effectively without repeating certain values. A partial solution was proposed using c(seq(1,5),...,seq(5,9)), but the goal remains to utilize only rep() and seq().

PREREQUISITES
  • Understanding of R programming language
  • Familiarity with vector operations in R
  • Knowledge of the rep() function in R
  • Knowledge of the seq() function in R
NEXT STEPS
  • Explore advanced usage of the rep() function in R
  • Learn how to combine seq() and rep() for complex vector generation
  • Investigate the use of loops in R for generating sequences
  • Study R's vector recycling rules and their implications on function outputs
USEFUL FOR

R programmers, data analysts, and students tackling vector manipulation challenges in R.

Mogarrr
Messages
120
Reaction score
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
well you should try this. rep()
{
i = 1;
seq(i,i+4);
i++;
}
and check when i>5 then exit.
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
929
  • · Replies 32 ·
2
Replies
32
Views
5K
  • · Replies 8 ·
Replies
8
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 9 ·
Replies
9
Views
3K
Replies
11
Views
2K