[R] Problems with merge

arun smartpink111 at yahoo.com
Wed May 29 17:05:31 CEST 2013


data1$A[8]
#[1] 1.4
 data2$A[15]
#[1] 1.4
 data2$A[15]==data1$A[8]
#[1] FALSE

You can check these links:
http://r.789695.n4.nabble.com/Comparing-decimal-numbers-td3251437.html
http://rwiki.sciviews.org/doku.php?id=misc:r_accuracy

A.K.

>The FAQ says "Other numbers have to be rounded to (typically) 53 binary digits accuracy." 
>These numbers are clearly a bit less than that. Also, I've defined 
these numbers, they were not calculated through a multiplication or 
division >operation. I would hate to think that R would have trouble with
 such a basic operation. I must be doing something wrong. 



----- Original Message -----
From: arun <smartpink111 at yahoo.com>
To: R help <r-help at r-project.org>
Cc: 
Sent: Wednesday, May 29, 2013 2:33 AM
Subject: Re: Problems with merge

HI,
Possibly R FAQ: 7.31

 data1New<-data1
 data1New$A<- round(data1New$A,2)
 data2New<- data2
 data2New$A<- round(data2New$A,2)

merge(data1New,data2New,by="A")
#    A   B    C
#1 0.0 0.9 10.0
#2 1.1 0.6 11.1
#3 1.4 0.7 11.4
#4 3.1 0.4 13.1 
#5 4.4 0.8 14.4
A.K.



Hello, 

Lets say we have these two data frames: 

data1<-data.frame(c(1.23,1.363332,6.43209,4.230593,3.10294,5.09333,1.1,1.4,4.4,0),seq(0,by=0.1,length.out=10)) 
names(data1)<-c("A","B") 

data2<-data.frame(seq(0,6,by=0.1),seq(10,16,by=0.1)) 
names(data2)<-c("A","C") 

And we would like to merge only the similar values from column "A". 

merge(data1,data2,by="A",all=FALSE) 

we get: 

    A   B    C 
1 0.0 0.9 10.0 
2 1.1 0.6 11.1 
3 4.4 0.8 14.4 

Unfortunately, it's missing the value of 1.4 in both data.frame columns "A". 

Similarly, if we instead use: 

merge(data1,data2,by="A",all=TRUE) 

the value of 1.4 is duplicated while 0, 1.1, and 4.4 is not. 
All of the mathematical functions work as they should on the 1.4, 
yet merge seems to think that the two 1.4 in the different data frames 
are completely different values. 
I must be going crazy, because I use the merge function all the time and I've never run into this problem before. 

Thanks.  




More information about the R-help mailing list