Message-ID: <941871A13165C2418EC144ACB212BDB0BEB552@dshsmxoly1504g.dshs.wa.lcl>
Date: 2009-11-17T23:37:33Z
From: Nordlund, Dan (DSHS/RDA)
Subject: :Problem with Looping
In-Reply-To: <20091117181742.ofiev4kxi8o440cw@cubmail.cc.columbia.edu>
> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
> Behalf Of Bruno Giovannetti
> Sent: Tuesday, November 17, 2009 3:18 PM
> To: r-help at r-project.org
> Subject: [R] :Problem with Looping
>
> Hello,
>
> Sometimes the looping (using "for") seems to skip some iterations.
>
> An example:
>
> arg <- matrix(NA,length(seq(.30,.5,.01)),1)
> for (i in seq(.30,.5,.01)) {
> arg[i*100-29] <- i
> }
> arg
>
> What is the problem with this coding, please?
>
> Thank you so much,
> --
> Bruno Cara Giovannetti, PhD Candidate
> Economics Department
> Columbia University
> Personal Webpage: www.columbia.edu/~bcg2108
Don't use floating point operations in your indexing. You may also want to change how you generate your sequence. This is a floating point representation problem. See FAQ 7.31. Try something like this
arg <- matrix(NA,length(seq(30,50,1)),1)
for (i in seq(30,50,1)) {arg[i-29] <- i/100 }
Hope this is helpful,
Dan
Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204