R's seq in TypeScript

How to use

Install via yarn add locutus and import: import { seq } from 'locutus/r/base/seq'.

Or with CommonJS: const { seq } = require('locutus/r/base/seq')

Use a bundler that supports tree-shaking so you only ship the functions you actually use. Vite, webpack, Rollup, and Parcel all handle this. For server-side use this is less of a concern.

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

#codeexpected result
1seq(5)[1, 2, 3, 4, 5]
2seq(1, 5, 2)[1, 3, 5]
3seq(1, 2, undefined, 5)[1, 1.25, 1.5, 1.75, 2]

Notes

  • Generates numeric sequences, similar to common R seq() forms.

  • Supported forms: seq(to), seq(from, to), seq(from, to, by), seq(from, to, undefined, length_out).

Here's what our current TypeScript equivalent to R's seq looks like.

export function seq(from: number, to?: number, by?: number, length_out?: number): number[] {
// discuss at: https://locutus.io/r/seq/
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Generates numeric sequences, similar to common R seq() forms.
// note 2: Supported forms: seq(to), seq(from, to), seq(from, to, by), seq(from, to, undefined, length_out).
// example 1: seq(5)
// returns 1: [1, 2, 3, 4, 5]
// example 2: seq(1, 5, 2)
// returns 2: [1, 3, 5]
// example 3: seq(1, 2, undefined, 5)
// returns 3: [1, 1.25, 1.5, 1.75, 2]

const start = Number(to === undefined ? 1 : from)
const end = Number(to === undefined ? from : to)
if (!Number.isFinite(start) || !Number.isFinite(end)) {
return []
}

const lengthOut = normalizeLengthOut(length_out)
if (lengthOut !== null) {
if (lengthOut <= 0) {
return []
}
if (lengthOut === 1) {
return [start]
}
const step = (end - start) / (lengthOut - 1)
const out: number[] = []
for (let i = 0; i < lengthOut; i++) {
out.push(roundNumber(start + step * i))
}
return out
}

const step = by === undefined ? (end >= start ? 1 : -1) : Number(by)
if (!Number.isFinite(step) || step === 0) {
throw new RangeError('seq(): by must be a non-zero finite number')
}

if ((end - start) * step < 0) {
return []
}

const out: number[] = []
const epsilon = Math.abs(step) * 1e-12
if (step > 0) {
for (let value = start; value <= end + epsilon; value += step) {
out.push(roundNumber(value))
}
} else {
for (let value = start; value >= end - epsilon; value += step) {
out.push(roundNumber(value))
}
}

return out
}

function normalizeLengthOut(value: number | undefined): number | null {
if (value === undefined) {
return null
}

const n = Number(value)
if (!Number.isFinite(n)) {
return null
}

return Math.trunc(n)
}

function roundNumber(value: number): number {
return Number.parseFloat(value.toFixed(12))
}

Improve this function

Locutus is a community effort following The McDonald's Theory: we ship first iterations, hoping others will improve them. If you see something that could be better, we'd love your contribution.

View on GitHub · Edit on GitHub · View Raw


We have 12 R functions so far - help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

  • Get inspiration from the R base documentation.
  • Click "New file" in the appropriate folder on GitHub. This will fork the project to your account, directly add the file to it, and send a Pull Request to us.

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.

« More R base functions


Star