Nikita
Nikita

Reputation: 33

Create the sequence by using an R-function

I need to create the following sequence:

> A
1, 2, 3, 11, 12, 13, 21, 22, 23, 31, 32, 33

> B
-1, 2, -3, 4, -5, 6, -7, 8, -9, 10

I've tried to use seq() function like this:

seq(1:3, 31:33, by = 10)

But it only allows to input 1 number in "from" and "to"

Maybe, someone knows how to use this or other functions to create the given sequence. Thank you in advance.

Upvotes: 0

Views: 44

Answers (2)

Ronak Shah
Ronak Shah

Reputation: 388972

You can use :

A <- c(sapply(seq(0, 30, 10), `+`, 1:3))
A
#[1]  1  2  3 11 12 13 21 22 23 31 32 33

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

Upvotes: 2

Cal Lee
Cal Lee

Reputation: 72

Here's one way to get to A: A <- rep(1:3, 3) + rep(c(0, 10, 20), each=3) or to generalize to n A <- rep(1:n, n) + rep(seq(0, by=10, length.out=n), each=n)

For B, I can't find anything truly elegant: B <- 1:10 * (-1)^(1:10 %% 2)

That's the best I got

Upvotes: 0

Related Questions