繁体   English   中英

数据帧R中值的最大一阶导数

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM