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

R: Find Minimum Value in Column of Data Frame that is Greater Than or Equal to Row Value of Column in a Different Data Frame

$
0
0

First time asking a question (be gentle) since I haven't been able to find anything that works.

In R I have two data frames. One (DataFrameA) has a column with a list of unique dates. The other (DataFrameB) also has a list of dates. But some dates in DataFrameB may not exist in DataFrameA. When that is the case, I want to update the date in DataFrameB to the minimum date from DataFrameA that is greater than the date in DataFrameB.

In SQL I'd probably do something like this:

Select MyDate as OldDate, 
(select min(MyDate) from DataFrameA where MyDate >= B.MyDate) as NewDate 
from DataFrameB as B

My goal is to update the MyDate Column in each row of DataFrameB with the correct value from DataFrameA.

So if DataFrame B starts out with

2019-01-01
2019-01-02
2019-01-03
2019-01-04
2019-01-05

and DataFrameA only has

2019-01-01
2019-01-03
2019-01-05

DataFrameB will end up looking like

2019-01-01
2019-01-03
2019-01-03
2019-01-05
2019-01-05

Thanks in advance.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



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