[R] why the same values cannot be judged to be the same in R

jim holtman jholtman at gmail.com
Fri Nov 13 11:35:14 CET 2009


I think you want to use '%in%' instead of '=='.  Try debugging (or
understanding) what is happening by evaluating the subexpressions of
the compares you have in your original posting.  Also take a look at
the 'recycling' of arguments and then you may understand what is
happening:

> data2[(data2$x1 %in% data_1$x1) & (data2$y1 %in% data_1$y1) & (data2$tag == 0),]
  areaid   x1   y1 tag
2      1 1.53 2.34   0
4      1 1.52 2.35   0
>


On Thu, Nov 12, 2009 at 10:04 PM, rusers.sh <rusers.sh at gmail.com> wrote:
> Hi Rusers,
>  I found sometimes that the same values cannot be judged to be the same in
> R. Anybody knows the probelm? I think i ignored some minor detail. Thanks.
> Here is the example.
> ############
> data1<-matrix(data=c(1,1.2,1.3,"3/23/2004",1,1.5,2.3,"3/22/2004",2,0.2,3.3,"4/23/2004",3,1.5,1.3,"5/22/2004"),nrow=4,ncol=4,byrow=TRUE)
> data1<-data.frame(data1);names(data1)<-c("areaid","x","y","date")
> data2<-matrix(data=c(1,1.22,1.32,1,  1.53,  2.34,1,  1.21,  1.37,1,  1.52,
> 2.35,2,  0.21,  3.33,2,  0.23,  3.35,3,  1.57, 1.31,3,  1.59,
> 1.33),nrow=8,ncol=3,byrow=TRUE)
> data2<-data.frame(data2);names(data2)<-c("areaid","x1","y1")
> data2$tag<-0
> data1_1<-data1[1,]
> data2_1<-data2[data2$areaid==data1_1$areaid & data2$tag==0,]
> ran_1<-sample(c(1:length(data2_1)),2, replace = FALSE)
> data2_1<-data2_1[ran_1,]
> data_1<-merge(data1_1,data2_1)
> #data_1
> #       areaid  x   y      date   x1   y1  tag
> #    1      1 1.2 1.3 3/23/2004 1.52 2.35   0
> #    2      1 1.2 1.3 3/23/2004 1.53 2.34   0
> data2[data_1$x1==data2$x1 & data_1$y1==data2$y1 & data2$tag==0,]
>  #data2[c(data_1$x1==data2$x1 & data_1$y1==data2$y1 & data2$tag==0),]
> #    areaid   x1    y1   tag
> #2      1    1.53  2.34   0
>  There should be two same observations between data_1 and data2, but here
> only one was identified.
>  Thanks a lot.
>
> --
> -----------------
> Jane Chang
> Queen's
>
>        [[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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list