[R] LOOping problem with R

Joshua Wiley jwiley.psych at gmail.com
Mon Aug 30 08:50:34 CEST 2010


Hi,

I would also like to point out that you flipped the first two the
values of theta in the R vs. FORTRAN versions.  This fixes part of
your differences (and makes it easy to see that the differences occur
when j < i, as David and Mario point out).

Josh

On Sun, Aug 29, 2010 at 10:27 PM, Nam Lethanh <namkyodai at gmail.com> 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 {   }).
>
> 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.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list