[R] Logical operator fails to recognize vector element

D. Alain dialvac-r at yahoo.de
Wed Oct 6 18:02:52 CEST 2010


Hi experts, 

I have encountered a strange phenomenon after loading spreadsheet data via
 
>example<-read.csv("M:\\...\\mydata.csv",header=T)

My data consist of two variables x and y which I have combined to a single vector using

>z<-rowSums(cbind(example$x,example$y))
>z
[1]  9.79  9.79 17.54 12.59  2.18  9.79 	#vector z with 6 elements

Now I want to identify all elements equal to 9.79, so I enter

>which(z==9.79)

And I get the result

[1] 2 6									#R failes to identify the fist element!

Taking a futher look at the elements I get the following:

> mode(z)
[1] "numeric"
> mode(z[1])
[1] "numeric"
> z[1]+z[2]								#I can add first and second element
[1] 19.58

But if I try to apply logical operators I get:

> z[1]==9.79
[1] FALSE
> z[2]==9.79
[1] TRUE
> z[1]==z[2]
[1] FALSE

When I reenter the first element via
> z[1]<-9.79
> z[1]==9.79
[1] TRUE

What is wrong with my data? Can anyone help?

> version
               _                            
platform         i386-pc-mingw32              
arch              i386                         
os                 mingw32                      
system          i386, mingw32                
status                                      
major             2                            
minor            11.1                         
year              2010                         
month           05                           
day               31                           
svn rev           52157                        
language        R                            
version.string  R version 2.11.1 (2010-05-31)

Thanks
D. Alain





More information about the R-help mailing list