简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM