# How to perform cartesian join for two data.table objects in R?

The cartesian join is the joining of two objects that creates the combination of each value in object with all the values in the other object. For example, if we have a vector x that contains 1, 2, 3 and the other object y contains a, b, c then the cartesian join will be 1a, 2a, 3a, 1b, 2b, 3b, 1c, 2c, and 3c. Check out the below examples to understand how it can be done.

## Example

> library(data.table)
> DT1<-data.table(x=1:4)
> DT1

## Output

   x
1: 1
2: 2
3: 3
4: 4

## Example

> DT2<-data.table(y=25:30)
> DT2

## Output

    y
1: 25
2: 26
3: 27
4: 28
5: 29
6: 30

Performing cartesian join of DT1 and DT2:

## Example

> DT1[,as.list(DT2),by=DT1]

## Output

   x  y
1: 1 25
2: 1 26
3: 1 27
4: 1 28
5: 1 29
6: 1 30
7: 2 25
8: 2 26
9: 2 27
10: 2 28
11: 2 29
12: 2 30
13: 3 25
14: 3 26
15: 3 27
16: 3 28
17: 3 29
18: 3 30
19: 4 25
20: 4 26
21: 4 27
22: 4 28
23: 4 29
24: 4 30
x y

Let’s have a look at another example:

## Example

> DT_G<-data.table(Group=LETTERS[1:5])
> DT_G

## Output

Group
1: A
2: B
3: C
4: D
5: E

## Example

> DT_Count<-data.table(sample(21:50,5))
> DT_Count

## Output

   V1
1: 22
2: 28
3: 45
4: 31
5: 47

Performing cartesian join of DT_G and DT_Count:

## Example

> DT_G[,as.list(DT_Count),by=DT_G]

## Output

Group V1
1: A 22
2: A 28
3: A 45
4: A 31
5: A 47
6: B 22
7: B 28
8: B 45
9: B 31
10: B 47
11: C 22
12: C 28
13: C 45
14: C 31
15: C 47
16: D 22
17: D 28
18: D 45
19: D 31
20: D 47
21: E 22
22: E 28
23: E 45
24: E 31
25: E 47
Group V1

Updated on: 19-Nov-2020

715 Views

##### Kickstart Your Career

Get certified by completing the course