[R] loop for in and decimals

Erik Iverson eriki at ccbr.umn.edu
Fri May 21 20:24:50 CEST 2010

Robert U wrote:
> Dear R-users,
> I'm trying to make the following loop: for (x in 0 : 10) and i would
> like x to be decimals rather than integers, giving a x range of e.g.
> 0.0 0.1 0.2 0.3 ... 9.9, 10. Would anyone know how to do that ?

Please start new threads when you have a new question, do not reply to 
existing threads and change the subject.

1) Use the seq function for what you want to do.

  seq(0, 10, by = 0.1)

In fact, according to ?colon,

For numeric arguments ‘from:to’ is equivalent to ‘seq(from, to)’,
      and generates a sequence from ‘from’ to ‘to’ in steps of ‘1’ or
      ‘1-’.  Value ‘to’ will be included if it differs from ‘from’ by an
      integer up to a numeric fuzz of about ‘1e-7’.

2) Perhaps you can avoid a for loop by using apply/sapply functions

3) Watch out for FAQ 7.31 depending on what you're doing with the values 
seq produces.

More information about the R-help mailing list