[R] how to make a series with decimal increment

Michael Prager Mike.Prager at noaa.gov
Wed Jun 14 21:23:31 CEST 2006


Gavin Simpson wrote on 6/14/2006 12:44 PM:
> On Tue, 2006-06-13 at 05:24 +0000, anil kumar rohilla wrote:
>   
>>   Hi List, 
>>        I am new to this Rsoftware, i want to make a sereis for example
>> which is having values like this, s<- 0,0.1,0.2,0.3,0.4........,1  
>>
>> [...]
> Hi Anil,
>
> see ?seq for the solution, which is:
>
> s <- seq(0, 1, by = 0.1)
> for(i in s) {
>   print(i)
> }
>   

I don't know if R avoids this somehow, but in many languages, generated 
noninteger sequences are sometimes one item shorter than expected, 
because of rounding errors.  For that reason, I would prefer to generate 
an integer sequence and then form a noninteger one from it.  For example:

 s  <- (1:10) / 10

Another option in R would be using the argument "length.out", as in

s = seq(from = 0.1, to = 1, length.out = 10)

...Mike


-- 
Michael H. Prager, Ph.D.
Population Dynamics Team
NOAA Center for Coastal Habitat and Fisheries Research
NMFS Southeast Fisheries Science Center
Beaufort, North Carolina  28516  USA
http://shrimp.ccfhrb.noaa.gov/~mprager/



More information about the R-help mailing list