Joining two data tables in R

r

#1

hello,

While reading an intro about the R package data.table I came across a line “The merge method of data.table is very similar to X[Y].” I wanted to know what this X[Y] signifies and how can we join two tables using it.

Thanks


#2

hello @adityashrm21,
X[Y] can be used to merge two data tables.
Once you load the data.table package into R:

> x <- data.table(a=1:3, b=2:4, key='a')
> x
   a b
1: 1 2
2: 2 3
3: 3 4 
> y <- data.table(a=1:3, c=c('a','b','c'), key='a')

Now merge them using merge once and then again using x[y]

> x[y]
   a b c
1: 1 2 a
2: 2 3 b
3: 3 4 c
> merge(x,y)
   a b c
1: 1 2 a
2: 2 3 b
3: 3 4 c

As you can see the result is same.
However the x[y] notation does not work with data frames and should be used only with data tables.
Hope this helps!!