Topic: Utilities

Topic Description:

Basic R utilities used in many packages and functions, such as date and string manipulations, statistical distributions, geometry of lines and distances. The R package date is required for the two data functions.



File: utilities/geometry.r

View File Source Download File No help file available

Function: pts.to.interceptslope

Function Description: pts.to.interceptslope

Returns intercept and slope of a line given two pairs of coordinates on the line. Arguments can be vectors; if both are vectors, must be same size. If the x's are exactly equal, so slope is infinite, it returns the x as the first argument.

Function Arguments:

ArgumentDefault Value
pt1
pt2

Function Source:

pts.to.interceptslope=function(pt1,pt2)
{
if(is.null(dim(pt1))) { x1=pt1[1]; y1=pt1[2] }
else { x1=pt1[,1]; y1=pt1[,2] }

if(is.null(dim(pt2))) { x2=pt2[1]; y2=pt2[2] }
else { x2=pt2[,1]; y2=pt2[,2] }

len=IfElse(length(x2)>length(x1),length(x2),length(x1))
exact=(x1==x2)

slope=(y2-y1)/(x2-x1)
inter=y1-slope*x1
slope[exact]=Inf
inter[exact]=x1[exact]

result=data.frame(b=inter,m=slope)
}