djMohit
djMohit

Reputation: 171

Extracting the elements from the consecutive pattern in the dataframe vector

I have a dataframe of more than 30,000 values as follows

1   qw
2   as
3   we
4   er
5   rt
6   @@@@
7   @@@@
8   @@@@
9   @@@@
10  as
11  df
12  fg
13  gh
14  hj

I want to extract the values at location index 1,3,5,10,12,14,19,21,23 and so on. As a beginner I know extraction of value using seq(first, last, by=) but I am not able to do slicing as per the above pattern.

1   qw
3   we
5   rt
10  as
12  fg
14  hj

Upvotes: 0

Views: 43

Answers (1)

JAQuent
JAQuent

Reputation: 1224

Having something nested like this would work for you:

> rep(seq(1, 5, 2), 3) + rep(seq(0, 18, 9), each = 3)
[1]  1  3  5 10 12 14 19 21 23

For more values:

nMax <- 30000

seq1 <- seq(1, 5, 2)
seq2 <- rep(seq(0, nMax, 9))

rep(seq1, length(seq2)) + rep(seq2, each = 3)

Upvotes: 1

Related Questions