【问题标题】:Full Join in dplyr完全加入 dplyr
【发布时间】:2020-05-28 17:01:40
【问题描述】:

我有一个看起来像这样的数据框:

library(tidyverse) 
df <- tibble::tribble(
        ~sub_date, ~period,
        "2019-01",       1,
        "2019-01",       2,
        "2019-01",       3,
        "2019-02",       1,
        "2019-02",       2,
        "2019-03",       1,
        "2019-03",       2,
        "2019-03",       3,
        "2019-03",       4
        )

  sub_date period
  <chr>     <dbl>
1 2019-01       1
2 2019-01       2
3 2019-01       3
4 2019-02       1
5 2019-02       2
6 2019-03       1
7 2019-03       2
8 2019-03       3
9 2019-03       4

还有一个:

period <- tibble::tribble(
            ~period, ~forecast,
                  1,        10,
                  2,        20,
                  3,        30,
                  4,        40,
                  5,        50,
                  6,        60,
                  7,        70
            )


  period forecast
   <dbl>    <dbl>
1      1       10
2      2       20
3      3       30
4      4       40
5      5       50
6      6       60
7      7       70

我正在努力以某种方式加入他们,以便在 df 中我可以填写表 period 中缺少的句点,也就是 period 中的行数 X df 中的不同 sub_date。

如下:

df_output <- tibble::tribble(
               ~sub_date, ~period, ~forecast,
               "2019-01",       1,        10,
               "2019-01",       2,        20,
               "2019-01",       3,        30,
               "2019-01",       4,        40,
               "2019-01",       5,        50,
               "2019-01",       6,        60,
               "2019-01",       7,        70,
               "2019-02",       1,        10,
               "2019-02",       2,        20,
               "2019-02",       3,        30,
               "2019-02",       4,        40,
               "2019-02",       5,        50,
               "2019-02",       6,        60,
               "2019-02",       7,        70,
               "2019-03",       1,        10,
               "2019-03",       2,        20,
               "2019-03",       3,        30,
               "2019-03",       4,        40,
               "2019-03",       5,        50,
               "2019-03",       6,        60,
               "2019-03",       7,        70
               )

# A tibble: 21 x 3
   sub_date period forecast
   <chr>     <dbl>    <dbl>
 1 2019-01       1       10
 2 2019-01       2       20
 3 2019-01       3       30
 4 2019-01       4       40
 5 2019-01       5       50
 6 2019-01       6       60
 7 2019-01       7       70
 8 2019-02       1       10
 9 2019-02       2       20
10 2019-02       3       30
# … with 11 more rows

我认为这是一个完全连接,但我没有得到想要的结果。

有什么帮助吗?

【问题讨论】:

    标签: r dplyr tidyverse tidyr


    【解决方案1】:

    您可以使用tidyr::crossing 获得您想要的结果:

    crossing(select(df, sub_date), period)
    

    请注意,您不是在寻找join,因为您希望sub_date 的每个组合与periodforecast 的每个组合都结合(或交叉)。

    【讨论】:

    • 你为什么使用selectcrossingdf 有多个列的情况下是否有效? @Cettt
    • 改用crossing(distinct(df, sub_date), df)
    • @xxxvincxxx,如果你使用多列,你会遇到冲突(Error: Column name period` 不能重复。), try and see. In this case, the other columns in df` 可能是一个红鲱鱼,因为它看起来像df$period 应该与period$period 匹配/加入,但是......输出并不表明这一点。
    【解决方案2】:

    您可以尝试合并表格吗?试试这个,看看它是否能满足您的需求?

    df <- df %>% distinct(sub_date) answer <- merge(periods, df, all = TRUE)

    【讨论】:

      猜你喜欢
      • 2019-06-16
      • 1970-01-01
      • 2011-06-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-14
      • 2015-04-07
      • 1970-01-01
      相关资源
      最近更新 更多