# R - Generate a sequence of numbers

I am trying to create sequences of number of 6 cases, but with 144 cases intervals.

Like this one for example

`c(1:6, 144:149, 288:293) 1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293 `

How could I generate automatically such a sequence with

`seq `

or with another function ?

I find the `sequence` function to be helpful in this case. If you had your data in a structure like this:

```(info <- data.frame(start=c(1, 144, 288), len=c(6, 6, 6))) # start len # 1 1 6 # 2 144 6 # 3 288 6 ```

then you could do this in one line with:

```sequence(info\$len) + rep(info\$start-1, info\$len) # [1] 1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293 ```

Note that this solution works even if the sequences you're combining are different lengths.

Here's one approach:

```unlist(lapply(c(0L,(1:2)*144L-1L),`+`,seq_len(6))) # or... unlist(lapply(c(1L,(1:2)*144L),function(x)seq(x,x+5))) ```

Here's a way I like a little better:

```rep(c(0L,(1:2)*144L-1L),each=6) + seq_len(6) ```

Generalizing...

```rlen <- 6L rgap <- 144L rnum <- 3L```

``` starters <- c(0L,seq_len(rnum-1L)*rgap-1L) ```

```rep(starters, each=rlen) + seq_len(rlen) # or... unlist(lapply(starters+1L,function(x)seq(x,x+rlen-1L))) ```

This can also be done using `seq` or `seq.int`

```x = c(1, 144, 288) c(sapply(x, function(y) seq.int(y, length.out = 6)))```

``` ```

```#[1] 1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293 ```

As @Frank mentioned in the comments here is another way to achieve this using @josilber's data structure (This is useful particularly when there is a need of different sequence length for different intervals)

`c(with(info, mapply(seq.int, start, length.out=len)))`

``` ```

```#[1] 1 2 3 4 5 6 144 145 146 147 148 149 288 289 290 291 292 293 ```

Category:r Views:65 Time:2019-03-23

## Related post

• Generate a sequence of numbers for a given range WITHOUT using a table from a query 2014-05-29

Ok. I need to generate a sequence of numbers from a query with a given minimum and maximum value to set the range. I have nothing to use to determine what those values may be in advance so a table, unless I want to create the entire number line (0 to

• Java: Generate a sequence of numbers within a range? 2011-04-16

I need to be able to generate a series of ascending or descending numbers within a range. public int nextInRange(int last, int min, int max, int delta, boolean descending) { delta *= descending ? -1 : 1; int result = last + delta; result %= max; retu

• Generate multiple sequences of numbers with unique values at each index 2012-01-12

I have a row with numbers 1:n. I'm looking to add a second row also with the numbers 1:n but these should be in a random order while satisfying the following: No positions have the same number in both rows No combination of numbers occurs twice For e

• How to produce range with step n in bash? (generate a sequence of numbers with increments) 2009-06-08

The way to iterate over a range in bash is for i in {0..10}; do echo \$i; done What would be the syntax for iterating over the sequence with a step? Say, I would like to get only even number in the above example. --------------Solutions------------- I

• Generate a sequence of numbers (powers) in order 2011-01-27

I'm looking for an algorithm (or better yet, code!) for a the generation of powers, specifically numbers with an odd exponent greater than 1: third powers, fifth powers, seventh powers, and so forth. My desired output is then 8, 27, 32, 125, 128, 216

• How to generate a sequence of numbers that can later be used in the command lines in python? 2011-10-28

what I want to do is to write a script wich get some files as input and then run some command on them! I first want to know how many files dose the user has and then ask for him/her to upload it and later I want to run some command on it. So I had us

• What are the tradeoffs when generating unique sequence numbers in a distributed and concurrent environment? 2010-07-08

I am curious about the contraints and tradeoffs for generating unique sequence numbers in a distributed and concurrent environment. Imagine this: I have a system where all it does is give back an unique sequence number every time you ask it. Here is

• Is there a way to generate a seed out of a sequence of numbers? 2012-01-20

For example if java produces the pseudorandom sequence: 9 3 2 5 6 by using 23 as a seed, how can I do the inverse? i.e. getting 23 out of the sequence 9 3 2 5 6. Or how do I assign a seed for a certain sequence? It is easy to do if there is a databas

• How do I generate this specific sequence of numbers? 2010-09-24

Apparently there is no predefined list available in .net. I'd like to use a number of standard colors, e.g. something like red, green, blue, yellow, ... i.e. the typical colors consisting of 00 and FF components, followed by those with additional 7F

• Computing A Specific Generating Function Sequence 2009-03-26

http://www.research.att.com/~njas/sequences/A097196 I was wondering how to continue generating the sequence given in the link. It is based off a generating function. Any ideas would be appreciated. Certain numbers in a certain sequence of this sequen

• What is the most efficient way to generate unique pseudo-random numbers? 2009-03-29

Duplicate: Unique random numbers in O(1)? I want an pseudo random number generator that can generate numbers with no repeats in a random order. For example: random(10) might return 5, 9, 1, 4, 2, 8, 3, 7, 6, 10 Is there a better way to do it other th

• Looking for an algorithm to spit out a sequence of numbers in a (pseudo) random order 2009-04-09

Suppose I have a sequence of numbers: {n, n+1, n+2, ... n + m} Without storing the numbers ahead of time I want to create a function f(), which given the sequence {1,2,3,...m} will spit out the original set in a random (or at least pseudo random) ord

• Emacs macro to generate a sequence? 2009-10-02

I was reading this article on how to create vi macros: vi sequence macro and I saw one useful macro to create a sequence in the buffer. 1. 2. ... 100. In vi, the trick is that one can hit ctrl-a over a number and it will add one to it, so this made t

• How do you generate cryptographically secure random numbers with PHP? 2009-10-11

Possible Duplicate: Secure random number generation in PHP We need to generate a cryptographically random string to use as an authentication token, which will be tied to session data in the database. We are using PHP, which doesn't appear to have a s

• Best Way to Generate Unique and consecutives numbers in Oracle 2009-12-31

I need to generate unique and consecutive numbers (for use on an invoice), in a fast and reliable way. currently use a Oracle sequence, but in some cases generated numbers are not consecutive because of exceptions that may occur. I thought a couple o

• Using boost::random and getting same sequence of numbers 2010-02-03

I have the following code: Class B { void generator() { // creating random number generator boost::mt19937 randgen(static_cast<unsigned int>(std::time(0))); boost::normal_distribution<float> noise(0,1); boost::variate_generator<boost::

• How to convert sequence of numbers in an array to range of numbers 2010-02-16

In javascript how to convert sequence of numbers in an array to range of numbers? eg. [2,3,4,5,10,18,19,20] to [2-5,10,18-20] --------------Solutions------------- Here is an algorithm that I made some time ago, originally written for C#, now I ported

• Generating "too perfect" random numbers 2010-03-06

A good RNG ought to pass several statistical tests of randomness. For example, uniform real values in the range 0 to 1 can be binned into a histogram with roughly equal counts in each bin, give or take some due to statistical fluctuations. These coun

• Generating two differnet pseudorandom numbers using the same seed 2010-06-02

Is it possible to generate two different psuedorandom numbers on two separate program runs without using time as the seed? i.e. using the same seed on both runs, is it possible to get two different numbers? --------------Solutions------------- In gen

• generate random sequence and plot in R 2010-08-25

I would like to generate a random sequence composed of 3000 points, which follows the normal distribution. The mean is c and the standard deviation is d. But I would like these 3000 points lies in the range of [a,b]. Can you tell me how to do it in R

• generate a sequence array in fortran 2011-01-14

Is there an intrinsic in Fortran that generates an array containing a sequence of numbers from a to b, similar to python's range() >>> range(1,5) [1, 2, 3, 4] >>> range(6,10) [6, 7, 8, 9] ? --------------Solutions------------- No, t

• PHP - need to follow awkward criteria when generating random sequence 2011-06-28

I am having a hard time wrapping my head around what I need to do to generate a sequence of non-repeating integers within a given range that meets the specific criteria that I have. So, here are the criteria: Use only the numbers between 1 and MAX (l

• Generate N quasi random numbers in less than O(N) 2012-04-08

This was inspired by a question at a job interview: how do you efficiently generate N unique random numbers? Their security and distribution/bias don't matter. I proposed a naive way of calling rand() N times and eliminating dupes by trial and error,

• Does anyone know where there is c# code or a dll which can generate sample credit card numbers 2009-06-08

For an application we are working on I need to generate sample credit card numbers which pass the Luhn algorithm but which are also unique, so we cannot use the sample credit card numbers. We need to be able to generate around 300 card numbers at a t

• Generating non-uniform random numbers 2009-06-10

Can you tell me any ways to generate non-uniform random numbers? I am using Java but the code examples can be in whatever you want. One way is to create a skewed distribution by adding two uniform random numbers together (i.e. rolling 2 dice). ------