# Sequence generation in R, Python and Julia

**R snippets**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Recently I was comparing implementation of sequence generation functions in R, Python (numpy) and Julia. Interestingly even such basic functions have slight differences in implementation. In my opinion Julia provides the best solution and Python the worst.

The test case will be a 6 element sequence from 0.1 to 1.1 namely [0.1, 0.3, 0.5, 0.7, 0.9, 1.1]. In all languages we can generate it in three ways: by direct specification, by sequence giving its length and by sequence giving its step (there actually three flavors here: with exact and inexact right bound).

The test codes follow but let me first give the conclusions:

R:

- seq will generate the same sequence in all cases (even if to parameter is a bit too small – see seq help for details about ‘just beyond’, but then the last value in the sequence is corrected);
- the sequence is not guaranteed to be equal to manually entered literals that represent the sequence most exactly;
- if you generate the same sequence in the reversed order the result does not have to be equal.

Python:

- it matters if we generate sequence using linspace or arange;
- arange excludes right end of the range specification; this actually can result in unexpected results; check numpy.arange(0.2, 0.6, 0.4) vs numpy.arange(0.2, 1.6, 1.4);
- the sequence is not guaranteed to be equal to manually entered literals that represent the sequence most exactly;
- if you generate the same sequence in the reversed order the result does not have to be equal.

Julia:

- it may be important if we supply exact value of the last element of the sequence or it is inexact when we specify step (equivalent of by in R), check out function colon in range.jl;
- sequence generated by step and by number of elements can differ even if they have the same endpoints;
- Julia actually is able to generate sequence equal to sequence that is equal to manually entered literals (sequence using step with exact right bound);
- generating the sequence in the reversed order gives you the same result.

Below you can check the codes I used. Here are the results for R:

x <- c(0.1, 0.3, 0.5, 0.7, 0.9, 1.1)

y1 <- seq(from = 0.1, to = 1.1, length.out = 6)

y2 <- seq(from = 0.1, to = 1.1, by = 0.2)

y3 <- seq(from = 0.1, to = 1.2, by = 0.2)

y4 <- seq(from = 0.1, to = 1.099999999999, by = 0.2)

print(x == y1)

# [1] TRUE FALSE TRUE FALSE TRUE TRUE

print(x == y2)

# [1] TRUE FALSE TRUE FALSE TRUE TRUE

print(y1 == y2)

# [1] TRUE TRUE TRUE TRUE TRUE TRUE

print(y2 == y3)

# [1] TRUE TRUE TRUE TRUE TRUE TRUE

print(y2 == y4)

# [1] TRUE TRUE TRUE TRUE TRUE FALSE

print(rev(seq(1.1,0.1, len=6)) == seq(0.1, 1.1, len=6))

# [1] TRUE TRUE TRUE TRUE FALSE TRUE

Now comes Python:

**leave a comment**for the author, please follow the link and comment on their blog:

**R snippets**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.