Merging files gives memory allocation error I have two files like

&gt; head(a)
  chr position
1   1   136962
2   1   562020
3   1   672948
&gt; class(a)
[1] "data.frame"
&gt;


And

&gt; dim(b)
[1] 1855235      10
&gt; head(b[,1:3])
            SNP chr position
1:  rs62635286          1    13116
2:  rs75454623          1    14930
3:    rs806731          1    30923
4: rs200943160          1    49298
5: rs116400033          1    51479
6: rs141149254          1    54490
&gt; class(b)
[1] "data.table" "data.frame"
&gt;


I want to find corresponding SNP in file b matched to position in file a so I did like this that gives memory allocation error. 

&gt; unique(merge(a,b,x.by=a$position,y.by=b$position)) 
Error: cannot allocate vector of size 137.2 Gb


I also tried rstudio cloud but uploading the bigger file never completes

Can you help me with task please?

The expected output would be something like

rs62635286  1   13116
rs376723915 1   91515
rs147061536 1   92858
rs62642131  1   135982
rs371474651 1   158006
rs201293782 1   665401
rs138476838 1   668374
rs1401137   1   2063094
rs3128293   1   2065339



  EDITED


&gt; setDT(a)
&gt; setkey(a, chr, position)
&gt; setkey(b, chr, position)
Error in setkeyv(x, cols, verbose = verbose, physical = physical) : 
  some columns are not in the data.table: chr,position
&gt; setkey(b, Chromosome, Position)
&gt; a[b, nomatch=0]
Error in bmerge(i, x, leftcols, rightcols, roll, rollends, nomatch, mult,  : 
  Incompatible join types: x.chr (factor) and i.Chromosome (integer). Factor columns must join to factor or character columns.
&gt;
You could try something like this :

First you should work with the same class of object : data.table for fast merging operation

# Turning a to a data.table
setDT(a)
# b is already a data.table 


Then you might have to set the keys of both table : 

# I added chr in case you need to match the same position on a different chromosome.
setkey(a, chr, position)
setkey(b, chr, position)


Finally perform the joining operation :

a[b, nomatch=0]


Because you didn't provide the expected output, it might need some tuning !