Use of apply() in the pspline package
I am seeking some help in trying to find the value for the maximum first
derivative across all the rows in a data.matrix when using the pspline
package. My data and the commands I have used look like this:
## The x-axis values
x<-c(490,495,500,505,510,515,520,525,530)
The y-axis values are contained in a matrix where each row corresponds to
the y-value per sample.
V1 V2 V3
1 0.2318345 0.2329633 0.2432734
2 0.1808581 0.1844433 0.1960315
3 0.1722618 0.1615062 0.1766804
4 0.1743336 0.1669799 0.1818896
5 0.1772355 0.1735916 0.1800227
(THERE ARE 7 COLUMNS BUT ONLY THE FIRST 3 ARE SHOWN FOR CLARITY). I have
tried to create a function to get the desired value, and then use apply()
to get the corresponding values per row.
library("pspline")
y=read.table("y.txt", header=T, sep="\t")
bcol<-matrix(NA,ncol=1,nrow=5)
derivative<-function(x,y=c(0,0))
+ {
+ y.mtx=as.matrix(y)
+ max( diff (abs( predict(sm.spline(x,y.mtx))$ysmth)))
+ }
bcol[]<-apply(y.matrix, 1, derivative)
However I get the following error:
Error in tapply(seq(along = y), match(x, ux), function(x, y, w)
c(mean(y[x]), :
arguments must have same length
If I run
max( diff (abs( predict(sm.spline(x,y.mtx))$ysmth)/5))
... Everything works fine. Am I approaching this the wrong way? Any
comments/suggestions will be deeply appreciated.
No comments:
Post a Comment