如何按组和日期组合两个数据框

How to combine two data-frames by group and date

我想在 R 中合并两个数据帧 d1 和 d2。它应该按“ID”分组,但最后应该只有一列“日期”,以便可以进行进一步分析(它是重要的是要知道“数字”条目 (d2) 是否链接到“类型”条目 (d1) 或与它有多接近)

假设 d1 和 d2 是

d1 <- data.frame(ID = c(1,1,2,2,2,3), 
                 sex = c("male", "male", "male", "male", "male", "female"),
                 date = c("2016-06-21 08:15:00", "2016-06-21 23:00:00",
                          "2017-04-19 07:15:00", "2017-04-20 07:15:00", "2017-04-20 08:15:00",
                          "2017-08-20 23:15:00" ),
                 type = c("horse", "snake", "horse", "horse", "monkey", "fish"))

d2 <- data.frame(ID = c(1,1,1,2,2,3,3), 
                 date = c("2016-06-20 08:15:00", "2016-06-21 22:15:00", "2016-06-22 08:45:00",
                          "2017-04-19 07:15:00", "2017-04-19 09:30:00",
                          "2017-08-19 08:15:00", "2017-08-21 06:30:00"),
                 number = c(65,234,64,234,6243,23,243))

我想要的输出是

d12 <- data.frame(ID = c(1,1,1,1,1,2,2,2,2,3,3,3),
                  sex = c(NA, "male", NA, "male", NA, "male", NA, "male", "male" , NA, "female", NA),
                  date = c("2016-06-20 08:15:00","2016-06-21 08:15:00", "2016-06-21 22:15:00", "2016-06-21 23:00:00", "2016-06-22 08:45:00",
                          "2017-04-19 07:15:00", "2017-04-19 09:30:00", "2017-04-20 07:15:00", "2017-04-20 08:15:00",
                          "2017-08-19 08:15:00", "2017-08-20 23:15:00", "2017-08-21 06:30:00" ),
                  type = c(NA, "horse", NA, "snake", NA, "horse", NA, "horse", "monkey", NA, "fish", NA),
                  number= c(65, NA, 234, NA, 64, 234, 6243, NA, NA, 23, NA, 243))

我试过这样做

d12 <- full_join(
  d1 %>% group_by(ID) %>% mutate(time=row_number()),
  d2 %>% group_by(ID) %>% mutate(time=row_number()),
by = c("ID", "time"))

但或多或少我只是添加了一些列,我并没有真正接近我想要的输出。非常期待您的帮助。

这个问题我还是有点不解。你可以试试

library(dplyr)

d1 %>% 
  full_join(d2, by = c("ID", "date")) %>% 
  mutate(date = as.POSIXct(date)) %>% 
  arrange(ID, date) %>% 
  tibble() # this line isn't really necessary

这个returns

      ID sex    date                type   number
   <dbl> <chr>  <dttm>              <chr>   <dbl>
 1     1 NA     2016-06-20 08:15:00 NA         65
 2     1 male   2016-06-21 08:15:00 horse      NA
 3     1 NA     2016-06-21 22:15:00 NA        234
 4     1 male   2016-06-21 23:00:00 snake      NA
 5     1 NA     2016-06-22 08:45:00 NA         64
 6     2 male   2017-04-19 07:15:00 horse     234
 7     2 NA     2017-04-19 09:30:00 NA       6243
 8     2 male   2017-04-20 07:15:00 horse      NA
 9     2 male   2017-04-20 08:15:00 monkey     NA
10     3 NA     2017-08-19 08:15:00 NA         23
11     3 female 2017-08-20 23:15:00 fish       NA
12     3 NA     2017-08-21 06:30:00 NA        243