R:匹配和重复出现

R: Matching and repeating occurence

(下面的示例代码)我有两个数据集。一个是产品库,另一个是客户 ID、日期和查看的产品,另一个 detail.I 想要合并,我在每个 ID 和日期中看到所有产品库以及匹配的位置。我试过使用 full_join 和合并以及左右连接,但它们不重复行。下面是我想要实现的示例。

id=c(1,1,1,1,2,2)
 date=c(1,1,2,2,1,3)
 offer=c('a','x','y','x','y','a')
 section=c('general','kitchen','general','general','general','kitchen')
 t=data.frame(id,date,offer,section)

offer=c('a','x','y','z')
 library=data.frame(offer)
######
t table
  id date offer section
1  1    1     a general
2  1    1     x kitchen
3  1    2     y general
4  1    2     x general
5  2    1     y general
6  2    3     a kitchen

library table
  offer
1     a
2     x
3     y
4     z

我想得到这个:

  id date offer section
1  1    1     a general
2  1    1     x kitchen
3  1    1     y  NA
4  1    1     z general
...

(必须有 6*4 个观察值) 我意识到因为我按要约匹配它不会像这样重复这些值,但是这样做的另一种选择是什么?非常感谢!!

您可以使用 tidyrdplyr 来获取数据。 crossing() 函数将创建您传入的变量的所有组合

library(dplyr)
library(tidyr)
t %>% 
  select(id, date) %>% 
  {crossing(id=.$id, date=.$date, library)} %>% 
  left_join(t)

您可以使用 complete 获取每个 iddatelibrary$offer 的所有组合。

tidyr::complete(t, id, date, offer = library$offer)

# A tibble: 24 x 4
#      id  date offer section
#   <dbl> <dbl> <chr> <chr>  
# 1     1     1 a     general
# 2     1     1 x     kitchen
# 3     1     1 y     NA     
# 4     1     1 z     NA     
# 5     1     2 a     NA     
# 6     1     2 x     general
# 7     1     2 y     general
# 8     1     2 z     NA     
# 9     1     3 a     NA     
#10     1     3 x     NA     
# … with 14 more rows