将数据帧扩展 n 次并添加列编号复制 1 到 n

Expand a dataframe n times and add a column numbering replicates 1 to n

一个简单的问题可能但无法弄清楚。我想通过复制前者来连接两个表。我尝试了 dplyr join 函数,但它们似乎没有在下面的示例中添加类别列。感谢任何帮助。

  > # I have two tables
    > 
    > table1
      Place Round Value
    1     A     1 12397
    2     A     2 18413
    3     A     3  7351
    4     A     4  5820
    5     B     1  3874
    6     B     2 10140
    7     B     3 10073
    8     B     4  7379
    > 
    > table2
      Place Category
    1     A        1
    2     A        2
    3     A        3
    4     B        1
    5     B        2
    6     B        3
    > 
    > # I want to add the category column from table2 and expand table1 as follows
    > 
    > final_table
       Place Round Value Category
    1      A     1 12397        1
    2      A     2 18413        1
    3      A     3  7351        1
    4      A     4  5820        1
    5      B     1  3874        1
    6      B     2 10140        1
    7      B     3 10073        1
    8      B     4  7379        1
    9      A     1 12397        2
    10     A     2 18413        2
    11     A     3  7351        2
    12     A     4  5820        2
    13     B     1  3874        2
    14     B     2 10140        2
    15     B     3 10073        2
    16     B     4  7379        2
    17     A     1 12397        3
    18     A     2 18413        3
    19     A     3  7351        3
    20     A     4  5820        3
    21     B     1  3874        3
    22     B     2 10140        3
    23     B     3 10073        3
    24     B     4  7379        3

我们可以使用 crossing

library(tidyr)
library(dplyr)
crossing(table1, table2[2]) %>%
    arrange(Category)
# A tibble: 24 x 4
#   Place Round Value Category
#   <chr> <int> <int>    <int>
# 1 A         1 12397        1
# 2 A         2 18413        1
# 3 A         3  7351        1
# 4 A         4  5820        1
# 5 B         1  3874        1
# 6 B         2 10140        1
# 7 B         3 10073        1
# 8 B         4  7379        1
# 9 A         1 12397        2
#10 A         2 18413        2
# … with 14 more rows

数据

table1 <- structure(list(Place = c("A", "A", "A", "A", "B", "B", "B", "B"
), Round = c(1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L), Value = c(12397L, 
18413L, 7351L, 5820L, 3874L, 10140L, 10073L, 7379L)), 
  class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6", "7", "8"))

table2 <- structure(list(Place = c("A", "A", "A", "B", "B", "B"), 
  Category = c(1L, 
2L, 3L, 1L, 2L, 3L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))