[R] Problems with merge

ggrothendieck@yifan.net ggrothendieck at yifan.net
Sat Mar 16 06:12:29 CET 2002


> # this works ok
> data(iris)
> merge(iris[1,],iris)
  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1          5.1         3.5          1.4         0.2  setosa
> 
> # but try the same thing with this data frame
> d.df <- data.frame(x=1:3,y=c("A","D","E"),z=c(6,9,10))
> d.df
  x y  z
1 1 A  6
2 2 D  9
3 3 E 10
> 
> # this results in zero rows whereas it should have 1 !!!
> merge(d.df[1,],d.df)
[1] x y z
<0 rows> (or 0-length row.names)
> 
> # remove last case and it suddenly works OK
> merge(d.df[1,],d.df[-3,])
  x y z
1 1 A 6
> 
> # or remove z and it suddenly works OK
> merge(d.df[1,-3],d.df[,-3])
  x y
1 1 A
> 
> # Here is another different problem with merge
> # we specified no sorting but the rows are not in the same order as e.df
> e.df <- data.frame(x=c(1,4,5,1,3,5),y=c("A","D","E","A","C","E"),z=c(6,9,10,6,8,10))
> e.df
  x y  z
1 1 A  6
2 4 D  9
3 5 E 10
4 1 A  6
5 3 C  8
6 5 E 10
> merge(e.df,unique(e.df),sort=F)
  x y  z
1 1 A  6
2 1 A  6
3 4 D  9
4 5 E 10
5 5 E 10
6 3 C  8
> 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list