根据r中的两个数据框选择基于日期的行(特定日期之前和之后的一天)

根据r中的两个数据框选择基于日期的行(特定日期之前和之后的一天),r,dataframe,dplyr,R,Dataframe,Dplyr,我有两个数据框,如下所示: anom_df: date id country anoms 2017-01-01 26 US 0 2017-01-02 26 US 0 2017-01-03 26 US 9 2017-01-04 26 US 0 2017-01-05 26 US 0 2017-01-06 26

我有两个数据框,如下所示:

anom_df:

 date         id  country     anoms
 2017-01-01   26      US        0
 2017-01-02   26      US        0
 2017-01-03   26      US        9
 2017-01-04   26      US        0
 2017-01-05   26      US        0
 2017-01-06   26      US        0
 2017-01-07   26      US        0
 2017-01-08   26      US        0
 2017-01-09   26      US        100
 2017-01-10   26      US        0
大师是什么

date         id     country    value     
 2017-01-01   26      US        2            
 2017-01-02   26      US        4             
 2017-01-03   26      US        9             
 2017-01-04   26      US        2             
 2017-01-05   26      US        4             
 2017-01-06   26      US        1
 2017-01-07   26      US        5
 2017-01-08   26      US        3
 2017-01-09   26      US        100
 2017-01-10   26      US        4 
我想从master_df创建第三个表,并与anom_df连接,以仅选择在anom$表的anom列中有值的日期以及master_df中该日期的前一天和后一天

最后,我想要下表

 date         id  country     value
 2017-01-02   26      US        2
 2017-01-03   26      US        9
 2017-01-04   26      US        4
 2017-01-08   26      US        3
 2017-01-09   26      US        100
 2017-01-10   26      US        4
我尝试了以下代码来查找入围的datafrmae:

before_after_anom<- dplyr::left_join(master_df,anom_df,by=c('id','country','date')) 
%>% mutate(diff = value - lag(value))
但是它没有给我正确的数据帧,你能帮我过滤它吗

图书馆管理员 我想从master_df创建第三个表并与 anom_df仅选择在中的anom列中具有值的日期 anom$表

这是一个半联接操作

硕士学位 阅读 日期前一天和日期后一天


但我不知道那是什么意思?有什么不同吗?

这不符合您的预期输出,但符合您的描述

library(dplyr)

left_join(master_df, anom_df, by = c('country', 'id', 'date')) %>%
  slice({inds <- which(anoms != 0); c(inds - 1, inds, inds + 1)}) %>%
  arrange(date)

#        date id country value anoms
#1 2017-01-02 26      US     4     0
#2 2017-01-03 26      US     9     9
#3 2017-01-04 26      US     2     0
#4 2017-01-07 26      US     5     0
#5 2017-01-08 26      US     3   100
#6 2017-01-09 26      US   100     0

我们在ANOM中找出非零索引,并在这些索引的上方和下方各选择一行

你能解释一下你的预期产出吗?如何在country='US'和相应值的情况下获得前3行作为输出?抱歉,我编辑了数据框。我想选择anom_dfI中有值的前一天、后一天以及某一天。我不明白如何加入semi_查找anom数据中有值的行的前一天和后一天,我还修改了主表。请检查一下
## Warning: 1 parsing failure.
## row col  expected    actual         file
##  10  -- 4 columns 5 columns literal data

## # A tibble: 10 x 4
##    date          id country value
##    <date>     <dbl> <chr>   <dbl>
##  1 2017-01-01    26 AF          2
##  2 2017-01-02    26 US          4
##  3 2017-01-03    26 US          9
##  4 2017-01-04    26 AF          2
##  5 2017-01-05    26 US          4
##  6 2017-01-06    26 BE          1
##  7 2017-01-07    26 US          5
##  8 2017-01-08    26 US          3
##  9 2017-01-09    26 US        100
## 10 2017-01-10    26 US          4
## # A tibble: 6 x 4
##   date          id country value
##   <date>     <dbl> <chr>   <dbl>
## 1 2017-01-02    26 US          4
## 2 2017-01-03    26 US          9
## 3 2017-01-05    26 US          4
## 4 2017-01-07    26 US          5
## 5 2017-01-08    26 US          3
## 6 2017-01-09    26 US        100
library(dplyr)

left_join(master_df, anom_df, by = c('country', 'id', 'date')) %>%
  slice({inds <- which(anoms != 0); c(inds - 1, inds, inds + 1)}) %>%
  arrange(date)

#        date id country value anoms
#1 2017-01-02 26      US     4     0
#2 2017-01-03 26      US     9     9
#3 2017-01-04 26      US     2     0
#4 2017-01-07 26      US     5     0
#5 2017-01-08 26      US     3   100
#6 2017-01-09 26      US   100     0