0

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.

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.