[R] LOOping problem with R
David Winsemius
dwinsemius at comcast.net
Mon Aug 30 08:33:42 CEST 2010
On Aug 30, 2010, at 1:27 AM, Nam Lethanh wrote:
> Dear Guys,
>
> I do converting codes from Fortran into R and got stuck in solving
> LOOPING
> procedure with R. In FORTRAN, it is (DO and END DO) for looping in
> the net.
> In R, it is (FOR with { }).
Looking at the results (namely the 1's from the fortran code) it
appears there are some conditions that cause FORTRAN not to enter the
loop whereas in R the inner loop is always entered. The numeric(0)'s
occur when you attempt to use a zero as an index. If you have a more
specific question about what the indices are at times that surprise,
then maybe you ought to put in more debugging code.
>
> I believe there is something wrong with my coding in R, do hope that
> you can
> help me solving following problems.
>
> It seems easy, but results are not the same.
>
> ***************************************************************************
> WITH R
>
> theta<-c(0.08,0.06,0.09,0)
> for (i in 1:4){
> for (j in 1:4){
> a<-1.0
> for (k in i:(j-1)){
> a<-a*theta[k]
> }
> print(a)
> }
> --------------------------------------------------------------------------------------
> HERE GOES the results
> -------------------------
> numeric(0)
> [1] 0.08
> [1] 0.0048
> [1] 0.000432
> numeric(0)
> [1] 0.0048
> [1] 0.06
> [1] 0.0054
> numeric(0)
> [1] 0.000432
> [1] 0.0054
> [1] 0.09
> numeric(0)
> [1] 0
> [1] 0
> [1] 0
> *********************************************************************************
> IN FORTRAN, the results is totally different.
>
> program calculating_a
> implicit none
> integer i, j, k
> double precision a, theta(4)
> theta(1)=0.06; theta(2)=0.08; theta(3)=0.09; theta(4)=0
> do i=1, 4
> do j=1, 4
> a=1
> do k=i,j-1
> a=a*theta(k)
> end do
>
> print*, a
>
> end do
> end do
> end
>
> Here goes the results with FOTRAN
>
> -------------------------------------------------------------------------------------
> 1
> 0.06
> 4.79E-003
> 4.31E-004
> 1
> 1
> 0.08
> 7.2E-0.03
> 1
> 1
> 1
> 0.09
> 1
> 1
> 1
> 1
> -----------------------------------------------------------------------------
>
>
>
> Thank you!
>
>
>
> Nam
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> 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