captcoma
captcoma

Reputation: 1898

Generate pairwise data.frame of all combinations of two data.frame with different number of rows

I have to dataframes a and b that I want to combine in a final dataframe c

a <- data.frame(city=c("a","b","c"),detail=c(1,2,3))

b <- data.frame(city=c("x","y"),detail=c(5,6))

the dataframe c should look like

  city.a detail.a city.b detail.b
1      a        1      x        5
2      a        1      y        6
3      b        2      x        5
4      b        2      y        6
5      c        3      x        5
6      c        3      y        6

I think I could use crossing from tidyr but for crossing(a,b) I get:

error: Column names `city`, `detail` must not be duplicated.
Use .name_repair to specify repair.

Upvotes: 1

Views: 164

Answers (3)

akrun
akrun

Reputation: 886948

With base R, we can use merge

merge(setNames(a, paste0(names(a), ".a")), b)
#   city.a detail.a city detail
#1      a        1    x      5
#2      b        2    x      5
#3      c        3    x      5
#4      a        1    y      6
#5      b        2    y      6
#6      c        3    y      6

Upvotes: 1

ThomasIsCoding
ThomasIsCoding

Reputation: 101099

Here is a base R solution by using rep() + cbind(), which gives duplicated column names:

C <- `row.names<-`(cbind(a[rep(seq(nrow(a)),each = nrow(b)),],b),NULL)

such that

> C
  city detail city detail
1    a      1    x      5
2    a      1    y      6
3    b      2    x      5
4    b      2    y      6
5    c      3    x      5
6    c      3    y      6

Or get a data frame having different column names by using data.frame():

C <- data.frame(a[rep(seq(nrow(a)),each = nrow(b)),],b,row.names = NULL)

such that

> C
  city detail city.1 detail.1
1    a      1      x        5
2    a      1      y        6
3    b      2      x        5
4    b      2      y        6
5    c      3      x        5
6    c      3      y        6

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 388817

Yes, crossing is the right function but as the error message suggests that column names should be not be duplicated try to change the column names

names(a) <- paste0(names(a), ".a")
names(b) <- paste0(names(b), ".b")
tidyr::crossing(a, b)

#  city.a detail.a city.b detail.b
#  <fct>     <dbl> <fct>     <dbl>
#1 a             1 x             5
#2 a             1 y             6
#3 b             2 x             5
#4 b             2 y             6
#5 c             3 x             5
#6 c             3 y             6

crossing is a wrapper over expand_grid so after correcting the names you can also use it directly.

tidyr::expand_grid(a, b)

Upvotes: 2

Related Questions