-7

I need to use seq() to create the vector (2,3,4,5,6,7,8,9,10,9,8,7,6,5,4,3,2), but I'm stuck. I've done quite a bit of YouTube-ing and reading online, but can't find a specific enough solution.

Any help is appreciated, but please use the seq function when making recommendations.

Jaap
  • 81,064
  • 34
  • 182
  • 193
sashi
  • 3
  • 1

3 Answers3

1

Using seq function, you need to following two steps:

Step-1 Generate sequence from 2 to 10 using following code:

a<-seq(from=2,to = 10)

Step-2 Generate sequence from 10 to 2 using following code:

b<-seq(from=9,to = 2)

Now, combine above two results using following code:

data<-c(a,b)

The output should as follow:

> data
[1]  2  3  4  5  6  7  8  9 10  9  8  7  6  5  4  3  2 

Hope it works for you!

Saurabh Chauhan
  • 3,161
  • 2
  • 19
  • 46
0

Try this:

unlist(mapply(seq, c(2,9), c(10,2), c(1,-1), SIMPLIFY = FALSE))
#  [1]  2  3  4  5  6  7  8  9 10  9  8  7  6  5  4  3  2
Sandipan Dey
  • 21,482
  • 2
  • 51
  • 63
0

According to help(":"),

[...] from:to is equivalent to seq(from, to), and generates a sequence from from to to in steps of 1 or -1.

This is the justification to offer

c(2:10, 9:2)
#[1]  2  3  4  5  6  7  8  9 10  9  8  7  6  5  4  3  2

as solution. Here, seq() is implicitely used but doesn't appear verbatim.

Uwe
  • 41,420
  • 11
  • 90
  • 134