1

I am using merge function on two data frames A and B

nrow(A) <- 11537
nrow(B) <- 734

But when I apply merge function as follows:

m <- merge(A,B,all.x=TRUE,by="id")

nrow(m) <- 29730

I get "m" with 29730 rows. "m" should have 11537 rows only as I am merging B into A. I am not able to identify reasons behind this. Can somebody please help me? What is getting added in "A"?

File is big, I cannot check manually.

Ayush Raj Singh
  • 863
  • 5
  • 16
  • 20
  • Can you make a small, reproducible example to demonstrate your problem? See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on how to do that efficiently. – Roman Luštrik Jun 25 '13 at 10:11
  • `nrow( x ) <- n` does not exist. How many columns with the same header do exist in your data files? You probably better clearly name the columns on which the merge shall take place. Without a reproducible example as pointed out by @Roman, no explanation possible! – vaettchen Jun 25 '13 at 10:24
  • @RomanLuštrik I have made a reproducible example, but how do I insert a table(data frame) here, there is no option? – Ayush Raj Singh Jun 25 '13 at 10:34
  • 1
    @user2474387 have you looked at the link I posted? You will find all the information there on how to make your example readily available to us. – Roman Luštrik Jun 25 '13 at 11:07

1 Answers1

2

If your id values aren't unique in each data.frame, then every combination of possible matches is created in the result. for example:

a = data.frame(id=c(1,1,1,2,2),val=1:5)
b = data.frame(id=c(1,1,3,2,2),valb=11:15)
m = merge(a,b,by="id",all.x=T)

m will have 10 rows - 6 with id=1 and 4 with id=2

My guess is this what causes your merged data.frame to become bigger than expected.

amit
  • 3,332
  • 6
  • 24
  • 32
  • Hi @amit, thank you. I got the problem. I have put a question related to this. http://stackoverflow.com/questions/17297021/fromjson-is-not-able-to-convert-some-entries-in-csv-format-and-showing-name can you look it up and see if you could help me? Thank you. – Ayush Raj Singh Jun 25 '13 at 18:44