[英]Maximum first derivative in for values in a data frame R
Good day, I am looking for some help in processing my dataset. 美好的一天,我正在寻找处理我的数据集的一些帮助。 I have 14000 rows and 500 columns and I am trying to get the maximum value of the first derivative for individual rows in different column groups.
我有14000行和500列,我试图获得不同列组中各行的一阶导数的最大值。 I have my data saved as a data frame with the first column being the name of a variable.
我将数据保存为数据框,第一列是变量的名称。 My data looks like this:
我的数据如下:
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
For each of the variables in the species column, I want to get the maximum derivative from Spec495 to Spec500 for example. 对于种类列中的每个变量,我想获得从Spec495到Spec500的最大导数。 This is what I did before I ran into errors.
这是我在遇到错误之前所做的。
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
I really wanted to avoid using a loop because of the time it takes, but this is the only way I know of thus far 我真的很想避免使用循环,因为它需要时间,但这是迄今为止我所知道的唯一方法
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) + }
This loop runs up until the 7861th value then get this error: 此循环运行直到7861th值然后得到此错误:
Error in smooth.Pspline(x = ux, y = tmp[, 1], w = tmp[, 2], method = method, :
NA/NaN/Inf in foreign function call (arg 6)
I am sure there must be a way to avoid using a loop, maybe using the plyr package, but I can't figure out how to do so, nor which package would be best to get the value for maximum derivative. 我确信必须有一种方法可以避免使用循环,也许使用plyr包,但我无法弄清楚如何这样做,也不知道哪个包最适合获得最大导数的值。
Can anyone offer some insight or suggestions? 任何人都可以提供一些见解或建议? Thanks in advance
提前致谢
First differences are the numerical analog of first derivatives when the x-dimension is evenly spaced. 第一个差异是当x维度均匀间隔时的一阶导数的数值模拟。 So something along the lines of:
所以有些东西:
which.max( diff ( predict(sm.spline(x, y))$ysmth) ) )
... will return the location of the maximum (positive) slope of the smoothed spline. ...将返回平滑样条曲线的最大(正)斜率的位置。 If you wanted the maximal slope allowing it to be either negative or postive you would use abs() around the predict()$ysmth.
如果你想要最大斜率允许它为负数或正数,你可以在predict()$ ysmth周围使用abs()。 If you are having difficulties with non-finite values then using an index of is.finite will clear both Inf and NaN difficulties:
如果您在使用非有限值时遇到困难,那么使用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.