Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 205343

How to merge data frame based on date between two intervals of time?

$
0
0

I'm working with two data frames. Data frame 1 includes a date, I'll refer to it as the merge_date. Data frame 2 includes two dates, start date and end date.

I want to merge data frame 1 and 2 together based their ID column and on whether the merge_date is between the start and end date in Data frame 2.

For example:

############## Make Data Frame 1 #############
ID <- c(2,4,6,8,10)
DF_1_Start_Date <- as.Date(c("1912-01-01", "1945-09-20", "1934-07-01", "1967-12-23", "1949-05-19"), tryFormats = c("%Y-%m-%d"))

DF_1 <- data.frame(ID, merge_date = DF_1_Start_Date)

############## Make Data Frame 2 #############

ID <- c(2,4,6,8,10)
DF_2_Start_Date <- as.Date(c("1911-01-01", "1944-09-20", "1933-07-01", "1963-12-23", "1948-05-19"), tryFormats = c("%Y-%m-%d"))

DF_2_End_Date <- as.Date(c("1913-01-01", "1946-09-20", "1935-07-01", "1970-12-23", "1952-05-19"), tryFormats = c("%Y-%m-%d"))

DF_2 <- data.frame(ID, interval_start = DF_2_Start_Date, interval_end = DF_2_End_Date)

########### Data Frame 1 and 2 ################
> DF_1
  ID merge_date
1  2 1912-01-01
2  4 1945-09-20
3  6 1934-07-01
4  8 1967-12-23
5 10 1949-05-19
> DF_2
  ID interval_start interval_end
1  2     1911-01-01   1913-01-01
2  4     1944-09-20   1946-09-20
3  6     1933-07-01   1935-07-01
4  8     1963-12-23   1970-12-23
5 10     1948-05-19   1952-05-19

I would like the data frame to look like:

DF_3
  ID merge_date   interval_start interval_end
1  2 1912-01-01   1911-01-01     1913-01-01
2  4 1945-09-20   1944-09-20     1946-09-20
3  6 1934-07-01   1933-07-01     1935-07-01
4  8 1967-12-23   1963-12-23     1970-12-23
5 10 1949-05-19   1948-05-19     1952-05-19

So that the IDs align and the merge_date merges between the interval_start and the interval_end dates.

Thank you!


Viewing all articles
Browse latest Browse all 205343

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>