简体   繁体   中英

R function to locate date interval and return specific value?

I am new to R and this is my first stackoverflow question!

I have the following data frames and I would like to create a function that accepts the "DocDate" from tbl_1, determines which interval from tbl_2 it falls within and assigns the "Period_Ending" value from tbl_2 in a new column.

library(tidyverse)
library(lubridate)


tbl_1 <- data.frame(DocDate = as.Date(c("2017-01-03", "2017-02-07", "2017-03-04")),
                    State1 = c("RI", "CT", "GA"),
                    MD = c(78, 115, 127),
                    OD = c(150, 220, 235))

tbl_1
  DocDate    State1  MD  OD
1 2017-01-03     RI  78 150
2 2017-02-07     CT 115 220
3 2017-03-04     GA 127 235



tbl_2 <- data.frame(begin = as.Date(c("2017-01-01", "2017-01-29", "2017-02-26")),
                    Period_Ending = as.Date(c("2017-01-29", "2017-02-26", "2017-03-26")))

tbl_2 <- tbl_2 %>% 
    mutate(date_interval = as.interval(x = weeks(4), start = (tbl_2$Period_Ending)))

tbl_2
        begin Period_Ending  date_interval
1 2017-01-01    2017-01-29   2017-01-29 UTC--2017-02-26 UTC
2 2017-01-29    2017-02-26   2017-02-26 UTC--2017-03-26 UTC
3 2017-02-26    2017-03-26   2017-03-26 UTC--2017-04-23 UTC

The desired outcome is a table which looks like this:

 DocDate       State1  MD  OD    Period_Ending
1 2017-01-03     RI    78  150    2017-01-29
2 2017-02-07     CT   115  220    2017-02-26
3 2017-03-04     GA   127  235    2017-03-26

I have bee able to do this manually using case_when statements like this

mutate(Period_Ending = case_when(
    (DocDate > ymd("2017-01-01")) & (DocDate < ymd("2017-01-29")) ~ ymd("2017-01-29"),
    (DocDate > ymd("2017-01-29")) & (DocDate < ymd("2017-02-26")) ~ ymd("2017-02-26"),
    (DocDate > ymd("2017-02-26")) & (DocDate < ymd("2017-03-26")) ~ ymd("2017-03-26")

, but I am hoping to find an automated solution and would greatly appreciate any help you could offer!

library(tidyverse)

# example data
tbl_1 <- data.frame(DocDate = as.Date(c("2017-01-03", "2017-02-07", "2017-03-04")),
                    State1 = c("RI", "CT", "GA"),
                    MD = c(78, 115, 127),
                    OD = c(150, 220, 235))

tbl_2 <- data.frame(begin = as.Date(c("2017-01-01", "2017-01-29", "2017-02-26")),
                    Period_Ending = as.Date(c("2017-01-29", "2017-02-26", "2017-03-26")))


tbl_2 %>%
  mutate(date = map2(begin, Period_Ending , ~seq(.x, .y, "day"))) %>%  # create a sequence of dates (begin to end)
  unnest(date) %>%                                                     # save it as column
  inner_join(tbl_1, by=c("date" = "DocDate")) %>%                      # join the other dataset
  select(DocDate = date, State1, MD, OD, Period_Ending)                # re-ararnge columns


# # A tibble: 3 x 5
#   DocDate    State1    MD    OD Period_Ending
#   <date>     <fct>  <dbl> <dbl> <date>       
# 1 2017-01-03 RI        78   150 2017-01-29   
# 2 2017-02-07 CT       115   220 2017-02-26   
# 3 2017-03-04 GA       127   235 2017-03-26 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM