[R] Usage of apply function with two matrices

Michal Szewczyk miszewczyk at tlen.pl
Fri Mar 12 12:49:41 CET 2010


Hello,

I am struggling to overcome following problem:

I have matrix Vf and matrix V, which both have 3 columns and I want to
create a spline function basing on coordinates from this matrices (more
precisely coordinates of function nr 1: x are taken from Vf[,1] and y are
taken from V[,1] respectively), because function apply() base on 1 argument
I made it in this way:

Vspline<-matrix(0,nrow=2*n,ncol=K)
Vspline<-rbind(Vf[,1:3],V[,1:3])

where n=5000 and it is number of rows in V and Vf matrices
and then:

FV<-apply(Vspline,2, function(z)
splinefun(Vspline[1:n,z],Vspline[(n+1):(2*n),z], method="mono",ties=mean))

The problems is, that when I am running it, following error occurs:

Error in xy.coords(x, y) : only 0's may be mixed with negative subscripts

I was trying to run function apply replacing z with particular values, and
it worked:

FV<-apply(Vspline,2, function(z)
splinefun(Vspline[1:n,1],Vspline[(n+1):(2*n),1], method="mono",ties=mean))
FV<-apply(Vspline,2, function(z)
splinefun(Vspline[1:n,2],Vspline[(n+1):(2*n),2], method="mono",ties=mean))
FV<-apply(Vspline,2, function(z)
splinefun(Vspline[1:n,3],Vspline[(n+1):(2*n),3], method="mono",ties=mean))

However it does not solve the problem, because I am not obtaining objects
FV[[1]], FV[[2]] and FV[[3]]. Object FV[[1]] is overwritten.

My questions are:
Why I am obtaining such an error?
Is there any function that works like apply, but enables me to use two
matrices as input data?

Thanks in advance for any help.

Greetings, Michal

-- 
View this message in context: http://n4.nabble.com/Usage-of-apply-function-with-two-matrices-tp1590442p1590442.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list