[R] LOOping problem with R

Mario Valle mvalle at cscs.ch
Mon Aug 30 08:34:56 CEST 2010


When j==1 for loops from i down to zero. 5:0 is valid and means c(5,4,3,2,1,0)
Hope it helps
			mario

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 {   }).
> 
> 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.

-- 
Ing. Mario Valle
Data Analysis and Visualization Group            | http://www.cscs.ch/~mvalle
Swiss National Supercomputing Centre (CSCS)      | Tel:  +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax:  +41 (91) 610.82.82



More information about the R-help mailing list