I have a function to calculate the inverse of a quadratic equation. By default it gives the two possible solutions:
invquad<-function(a,b,c,y,roots="both")
{
#Calculate the inverse of a quadratic function y=ax^2+bx+c (i.e. find x when given y.)
#Gives NaN with non real solutions.
root1<-sqrt((y-(c-b^2/(4*a)))/a)-(b/(2*a))
root2<--sqrt((y-(c-b^2/(4*a)))/a)-(b/(2*a))
if (roots=="both")
result<-c(root1,root2)
if (roots=="min")
result<-min(root1,root2)
if (roots=="max")
result<-max(root1,root2)
result
}
This works fine if given a single value of y, but if I give it a list or a column from a dataframe, then the min and max elements give me the minimum value of the whole list. I want it to return just the minimum result for that element. I'm assuming iterating over the list is possible, but it is not very efficient.
Any ideas ?