简体   繁体   English

生成具有不同行数的两个 data.frame 的所有组合的成对 data.frame

[英]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我必须将数据帧ab合并到最终数据帧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数据框c应该看起来像

  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:我想我可以使用tidyr交叉,但是对于crossing(a,b)我得到:

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

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是的, crossing是正确的功能,但由于错误消息表明不应重复列名,请尝试更改列名

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. crossing是对expand_grid的封装,因此在更正名称后,您也可以直接使用它。

tidyr::expand_grid(a, b)

Here is a base R solution by using rep() + cbind() , which gives duplicated column names:这是使用rep() + cbind()的基本 R 解决方案,它给出了重复的列名:

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() :或者使用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

With base R , we can use merge使用base R ,我们可以使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM