Reputation: 163
I have 2 tables and need to combine them using R as follows:-
Table 1
id
A
B
C
D
Table 2
group class
1 X
2 Y
I need a combined table like this:-
id group class
A 1 X
B 1 X
C 1 X
D 1 X
A 2 Y
B 2 Y
C 2 Y
D 2 Y
I tried many types of merges (like inner, left, full join etc) by creating dummy columns in the tables, but couldn't get the desired output. Any help will be of great help
Upvotes: 1
Views: 3085
Reputation: 521249
You are looking for a cross join between the two tables:
t1 <- data.frame(id=c("A", "B", "C", "D"))
t2 <- data.frame(group=c(1, 2), class=c("X", "Y"))
merge(t1, t2, all=TRUE)
id group class
1 A 1 X
2 B 1 X
3 C 1 X
4 D 1 X
5 A 2 Y
6 B 2 Y
7 C 2 Y
8 D 2 Y
Upvotes: 4