如何提取与df不匹配的行

时间:2017-06-07 11:12:55

标签: r

df1

Date  
2014-01-01 01:01:00  
2014-01-01 01:02:00  
2014-01-01 01:03:00  

df2  
Date                      **name**  
2014-01-01 01:01:00        **p1**  
2014-01-01 01:03:00        **p1**  
2014-01-01 01:01:00        **p2**  
2014-01-01 01:02:00        **p2**  

我们如何从df2获取缺失的行,即 对于“p1”缺少分钟02的时间序列,对于p2缺少03;

我尝试使用以下代码

a=merge(x = df1, y = df2, by = "Date", all.x = TRUE)

并且只获得错过日期

o / p

Date                    **name**  
2014-01-01 01:02:00     **p1**  
2014-01-01 01:03:00     **p2**

提前致谢

1 个答案:

答案 0 :(得分:1)

您可以尝试:

library(tidyverse) 
df2 %>% 
   semi_join(df1, by="Date") %>% 
   group_by(Date) %>%
   filter(n() < 2) %>% 
   mutate(Miss=ifelse(name== "p1", "p2", "p1"))
Source: local data frame [2 x 3]
Groups: V1 [2]

                 Date   name  Miss
               <fctr> <fctr> <chr>
1 2014-01-01 01:02:00     p2    p1
2 2014-01-01 01:03:00     p1    p2