1
我有一個函數來計算二次方程的倒數。默認情況下,它提供了兩種可能的解決方案:如何將Min或Max分別應用於函數的每個結果?
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
}
如果給y的單值能正常工作,但如果我給它一個列表或一個數據幀一列,然後最小和最大的元素給我整個列表的最小值。我希望它只返回該元素的最小結果。我假設迭代列表是可能的,但效率不高。
任何想法?
完美!就是我在找什麼.... – PaulHurleyuk 2009-09-30 14:34:28