Generate KDB / Q sequence similar to R seq (from, to, step)
Is there a way to generate a sequence of numbers with a given step, similar to the R seq (from, to, step) function?
eg.
> seq(1,20,2)
[1] 1 3 5 7 9 11 13 15 17 19
+3
aseidlitz
source
to share
4 answers
user2393012's answer is close, but not quite what you were looking for. The following works well -
q)seq:{x+z*til ceiling(1+y-x)%z}
q)seq[1;20;2]
1 3 5 7 9 11 13 15 17 19
+4
jgleeson
source
to share
Alternative (but not better than simple arithmetic solutions)
q){-1_(y>=)(z+)\x}[1;20;2]
1 3 5 7 9 11 13 15 17 19
+2
terrylynch
source
to share
Just use arithmetic :-)
q){[step;start;length] start+step*til length}[2;0;10]
0 2 4 6 8 10 12 14 16 18
q){[step;start;length] start+step*til length}[3;0;10]
0 3 6 9 12 15 18 21 24 27
+1
Manish patel
source
to share
Another option (a slight modification of terrylynch's solution ):
q) {(z+)\[floor(y-x)%z;x]} [1;20;2]
1 3 5 7 9 11 13 15 17 19
+1
Rahul
source
to share