a1
and b1
have to be combined into one data.frame before the unique()
function is applied. Otherwise, the vectors have different lengths.
DF <- data.frame(a1, b1)
unique(DF)[rep(1:nrow(unique(DF)), each = nrow(DF)), ]
For the first dataset
a1 = c("a","b","c","d","a","b")
b1 = c(1,2,3,4,1,2)
the result is:
a1 b1
1 a 1
1.1 a 1
1.2 a 1
1.3 a 1
1.4 a 1
1.5 a 1
2 b 2
2.1 b 2
2.2 b 2
2.3 b 2
2.4 b 2
2.5 b 2
3 c 3
3.1 c 3
3.2 c 3
3.3 c 3
3.4 c 3
3.5 c 3
4 d 4
4.1 d 4
4.2 d 4
4.3 d 4
4.4 d 4
4.5 d 4
consisting 24 rows (4 unique values in a1
times 6 which is the length of a1
)
Note that this different to user124123's answer which has 36 rows (length(b1)
times length(a1)
caused by rep(b1, each = length(a1))
.
For the second dataset
a1 = c("a","b","b","d","c","e","f","a","b","c","d")
b1 = c(1,1,1,2,3,2,3,1,1,3,2)
the result consists of 66 rows (6 unique values times 11).
(Output omitted for brevity).