[R] finding the intersection of two vectors

David Winsemius dwinsemius at comcast.net
Wed Jul 6 23:50:38 CEST 2011


On Jul 6, 2011, at 4:43 PM, Data Analytics Corp. wrote:

> Hi,
>
> Suppose I have two vectors, not necessarily the same length (in  
> fact, they usually are different lengths): y.1 that has increasing  
> values between 0 and 1; y.2 that has decreasing values between 1.0  
> and 0.  You can picture these as being supply (= y.1) and demand (=  
> y.2) curves from economics.  I typically plot these vectors on the  
> same graph against a common x variable, which happens to be price  
> for what I do.  The price variable runs from, say, $0 to $25.  When  
> I plot y.1 and y.2, I've been eye-balling a vertical line at a price  
> point where y.1 intersects y.2.  I'm now tired of eye-balling a line  
> through the intersection -- takes too much time to get it right or  
> just close enough.  I can't figure out how to find the price value  
> at which the two curves intersect.  Going back to the economics  
> interpretation, I want the price where supply equals demand.  Any  
> suggestions as to how I can find that price point in R?  Any  
> functions that help?

?approxfun # or..
?splinefun  #  should allow you to make two functions and then solve  
for the X taht minimizes the difference.

-- 

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list