[英]Maximum first derivative in for values in a data frame R
美好的一天,我正在尋找處理我的數據集的一些幫助。 我有14000行和500列,我試圖獲得不同列組中各行的一階導數的最大值。 我將數據保存為數據框,第一列是變量的名稱。 我的數據如下:
Species Spec400 Spec405 Spec410 Spec415
1 AfricanOilPalm_1_Lf_1 0.2400900 0.2318345 0.2329633 0.2432734
2 AfricanOilPalm_1_Lf_10 0.1783162 0.1808581 0.1844433 0.1960315
3 AfricanOilPalm_1_Lf_11 0.1699646 0.1722618 0.1615062 0.1766804
4 AfricanOilPalm_1_Lf_12 0.1685733 0.1743336 0.1669799 0.1818896
5 AfricanOilPalm_1_Lf_13 0.1747400 0.1772355 0.1735916 0.1800227
對於種類列中的每個變量,我想獲得從Spec495到Spec500的最大導數。 這是我在遇到錯誤之前所做的。
x<-c(495,500,505,510,515,520,525,530,535,540,545,550)##get x values of reflectance(Spec495 to Spec500)
y.data.f<-hsp[,21:32]##get row values for the required columns
y<-as.numeric(y.data.f[1,])##convert to a vector, for just the first row of data
library(pspline) ##Using a spline so a derivative maybe calculated from a list of numeric values
我真的很想避免使用循環,因為它需要時間,但這是迄今為止我所知道的唯一方法
for(j in 1:14900)
+ { y<-as.numeric(y.data.f[j,]) + a1d<-max(predict(sm.spline(x, y), x, 1))
+ write.table(a1d, file = "a1-d-appended.csv", sep = ",",
+ col.names = FALSE, append=TRUE) + }
此循環運行直到7861th值然后得到此錯誤:
Error in smooth.Pspline(x = ux, y = tmp[, 1], w = tmp[, 2], method = method, :
NA/NaN/Inf in foreign function call (arg 6)
我確信必須有一種方法可以避免使用循環,也許使用plyr包,但我無法弄清楚如何這樣做,也不知道哪個包最適合獲得最大導數的值。
任何人都可以提供一些見解或建議? 提前致謝
第一個差異是當x維度均勻間隔時的一階導數的數值模擬。 所以有些東西:
which.max( diff ( predict(sm.spline(x, y))$ysmth) ) )
...將返回平滑樣條曲線的最大(正)斜率的位置。 如果你想要最大斜率允許它為負數或正數,你可以在predict()$ ysmth周圍使用abs()。 如果您在使用非有限值時遇到困難,那么使用is.finite索引將清除Inf和NaN的困難:
predy <- predict(sm.spline(x, y))$ysmth
predx <- predict(sm.spline(x, y))$x
is.na( predy ) <- !is.finite(pred)
plot(predx, predy, # NA values will not blow up R plotting function,
# ... just create discontinuities.
main ="First Derivative")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.