[R] Odd behaviour of R

Roger Bivand Roger.Bivand at nhh.no
Fri Aug 18 11:02:57 CEST 2006


On Fri, 18 Aug 2006, Hiroto Miyoshi wrote:

> Dear R-users
> 
> I need your help. I am agonised by the odd behaviour of R
> described below
> 
> In the following program, data.frame "work" contains no rows,
> when Pb set to 0.3 and 0.7
> 
> data<-read.csv("output.csv",header=TRUE)
> for(N in c(20,40,80,160,320,640,1280)){
>    for(Pb in seq(0.1,0.9,0.1)){
>      work<-data[((data$N==N) & (data$Pb==Pb)),]
>      print(work)    
>    }
>  }
> 
> That is, when Pb set to 0.3 and 0.7,
> print(work) prints out the message "[1] N        Pb       a        
> b        c        d    outcomeA outcomeB <0 rows> (or 0-length row.names)"
> 
> However, data does exist when Pb is 0.3 or 0.7. 

This looks suspiciously like FAQ 7.31:

http://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f

and suggests that all.equal() may be a better test than "==" for an 
appropriate value of tolerance:

print(seq(0.1,0.9,0.1), 20)
seq(0.1,0.9,0.1)[7]==0.7
all.equal(seq(0.1,0.9,0.1)[7], 0.7)

> And
> when line "work<-data[((data$N==N) & (data$Pb==Pb)),]"
> is placed outside the "for" loops after Pb is manually set to 0.3,
> print(work) successfully prints out the appropriate data.
> Namely,
> 
> > N<-20
> > Pb<-0.3
> >      work<-data[((data$N==N) & (data$Pb==Pb)),]
> > work
>     N  Pb a b c  d outcomeA outcomeB
> 22 20 0.3 6 0 0 14    FALSE    FALSE
> 23 20 0.3 5 1 0 14    FALSE    FALSE
> 24 20 0.3 5 1 1 13    FALSE    FALSE
> ...................................................................
> 
> Why is this happen?
> I am using R version 2.3.0.
> Any help is greatly appreciated.
> 
> Thank you.
> 
> 
> ------------------------
> Hiroto Miyoshi
> h_m_ at po.harenet.ne.jp
> 
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-help mailing list