[R] customize the step value

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Fri Oct 29 18:16:17 CEST 2021


As others have replied, the customary way is to use the seq() function that
takes additional arguments besides a from= and a to= such as by= to specify
the step size and two others sometimes handy of length.out= and along.with=

In your case seq(from=1.5, to=3.5, by=0.5) works as well as the shorter
positional version of seq(1.5, 3.5, 0.5)

But as others have noted, certain calculations with floating point
arithmetic in pretty much any language can be imprecise in the final bits. I
doubt it matters for you but there are ways to do a comparison that allows
for a little leeway and still tests equal. 

The suggestion others have made is a good choice too, especially when you
know exactly what you need in advance and can adjust:

> seq(1.5, 3.5, 0.5)
[1] 1.5 2.0 2.5 3.0 3.5
> seq(3, 7) / 2
[1] 1.5 2.0 2.5 3.0 3.5
> 0.5*(3:7)
[1] 1.5 2.0 2.5 3.0 3.5


If you do this often and for larger vectors and efficiency matters, consider
using seq.int() in the latter cases as it is much faster when working on
just integers. 

-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Catherine Walt
Sent: Friday, October 29, 2021 3:06 AM
To: R mailing list <r-help using r-project.org>
Subject: [R] customize the step value

dear members,

Sorry I am newbie on R.
as we saw below:

> 1.5:3.5
[1] 1.5 2.5 3.5

How can I make the step to 0.5?
I want the result:

1.5 2.0 2.5 3.0 3.5

Thanks.
Cathy

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list