Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create sequence of numbers, excluding certain numbers

Tags:

r

sequence

I would like to create a sequence of 1:85, but excluding the numbers in seq(1,85,5). So like this:

2 3 4 5 7 8 9 10 12 13 etc..

What would be an efficient way to do this in R?

Many thanks!

like image 500
Rob Avatar asked Jun 26 '13 12:06

Rob


3 Answers

Using setdiff:

setdiff(1:85,seq(1,85,5))
 [1]  2  3  4  5  7  8  9 10 12 ...
like image 51
James Avatar answered Nov 15 '22 14:11

James


If the numbers you want to exclude can't be generalized, @HongOoi or @James answers are the way to go. But if they can be described by some mathematical test, Filter would be more efficient.

Filter(function(x) x %% 5 != 1, 1:85)
like image 6
Matthew Plourde Avatar answered Nov 15 '22 14:11

Matthew Plourde


(1:85)[-seq(1, 85, 5)]

or is that too obvious/inefficient?

like image 5
Hong Ooi Avatar answered Nov 15 '22 13:11

Hong Ooi