连接列,为新列中的每个变量复制现有行

Join columns, duplicating existing row for each variable in new column

好吧,假设我们有两列不同的数据。一个只是 运行 日期列:

day
2017-11-08              
2017-11-09              
2017-11-10              
2017-11-13              
2017-11-14              
2017-11-15  

另一个只是一个ID列

ID
asdflauih3298b43f9n
akjwn3ibfun9834n93n
nv43879n4vliuhs87ba

我想以某种方式加入这些列,以便每个 ID 每天都出现。像这样:

day            ID
2017-11-08     asdflauih3298b43f9n  
2017-11-08     akjwn3ibfun9834n93n
2017-11-08     nv43879n4vliuhs87ba  
2017-11-09     asdflauih3298b43f9n  
2017-11-09     akjwn3ibfun9834n93n      
2017-11-09     nv43879n4vliuhs87ba              
2017-11-10     asdflauih3298b43f9n  
2017-11-10     akjwn3ibfun9834n93n
2017-11-10     nv43879n4vliuhs87ba

希望在 tidyverse 中做到这一点。我知道这应该很容易,但我很难过。谢谢!

这可能是 crossing

的情况
tidyr::crossing(df1, df2)

-输出

# A tibble: 18 × 2
   day        ID                 
   <chr>      <chr>              
 1 2017-11-08 akjwn3ibfun9834n93n
 2 2017-11-08 asdflauih3298b43f9n
 3 2017-11-08 nv43879n4vliuhs87ba
 4 2017-11-09 akjwn3ibfun9834n93n
 5 2017-11-09 asdflauih3298b43f9n
 6 2017-11-09 nv43879n4vliuhs87ba
 7 2017-11-10 akjwn3ibfun9834n93n
 8 2017-11-10 asdflauih3298b43f9n
 9 2017-11-10 nv43879n4vliuhs87ba
10 2017-11-13 akjwn3ibfun9834n93n
11 2017-11-13 asdflauih3298b43f9n
12 2017-11-13 nv43879n4vliuhs87ba
13 2017-11-14 akjwn3ibfun9834n93n
14 2017-11-14 asdflauih3298b43f9n
15 2017-11-14 nv43879n4vliuhs87ba
16 2017-11-15 akjwn3ibfun9834n93n
17 2017-11-15 asdflauih3298b43f9n
18 2017-11-15 nv43879n4vliuhs87ba

数据

df1 <- structure(list(day = c("2017-11-08", "2017-11-09", "2017-11-10", 
"2017-11-13", "2017-11-14", "2017-11-15")), 
class = "data.frame", row.names = c(NA, 
-6L))

df2 <- structure(list(ID = c("asdflauih3298b43f9n", "akjwn3ibfun9834n93n", 
"nv43879n4vliuhs87ba")), class = "data.frame",
 row.names = c(NA, 
-3L))

您可以使用 dplyr 中的常规连接函数来执行 cross-join,方法是将 by 参数设置为 character():

library(dplyr)

left_join(A, B, by = character())

#> # A tibble: 18 × 2
#>    day        ID                 
#>    <chr>      <chr>              
#>  1 2017-11-08 asdflauih3298b43f9n
#>  2 2017-11-08 akjwn3ibfun9834n93n
#>  3 2017-11-08 nv43879n4vliuhs87ba
#>  4 2017-11-09 asdflauih3298b43f9n
#>  5 2017-11-09 akjwn3ibfun9834n93n
#>  6 2017-11-09 nv43879n4vliuhs87ba
#>  7 2017-11-10 asdflauih3298b43f9n
#>  8 2017-11-10 akjwn3ibfun9834n93n
#>  9 2017-11-10 nv43879n4vliuhs87ba
#> 10 2017-11-13 asdflauih3298b43f9n
#> 11 2017-11-13 akjwn3ibfun9834n93n
#> 12 2017-11-13 nv43879n4vliuhs87ba
#> 13 2017-11-14 asdflauih3298b43f9n
#> 14 2017-11-14 akjwn3ibfun9834n93n
#> 15 2017-11-14 nv43879n4vliuhs87ba
#> 16 2017-11-15 asdflauih3298b43f9n
#> 17 2017-11-15 akjwn3ibfun9834n93n
#> 18 2017-11-15 nv43879n4vliuhs87ba

reprex package (v2.0.1)

于 2022-05-24 创建

数据

A <- tibble::tribble(
  ~day,
  "2017-11-08",              
  "2017-11-09",              
  "2017-11-10",              
  "2017-11-13",              
  "2017-11-14",              
  "2017-11-15"  
)

B <- tibble::tribble(
  ~ID,
  "asdflauih3298b43f9n",
  "akjwn3ibfun9834n93n",
  "nv43879n4vliuhs87ba"
)