r - apply using multi-column data to produce a single numeric output -
r - apply using multi-column data to produce a single numeric output -
is possible utilize form of array , send apply on row-by-row basis? i've tried far results in error "incorrect number of dimensions" mutual error others have had here cannot find , illustration of how i'm attempting in testfunc2 below.
require(quantmod) getsymbols("spy",src="yahoo") ndata = 10 info = clcl(spy)[1:ndata,] testfunc1 = function(d1, x1, y1){ res1 = (d1 + 2*x1)^2 + y1 } #x1 constant - works x1 = .2 y1 = 1 tmp1 = apply(data, 1, testfunc1, x1, y1) result1 = cbind(data, x1, y1, tmp2) testfunc2 = function(z1, y1){ d1 = z1[,1] x1 = z1[,2] res2 = (d1 + 2*x1)^2 + y1 } x2 = xts(1:ndata, order.by=index(data)) z1 = cbind(data, x2) tmp2 = apply(z1, 1, testfunc2, y1) result2 = cbind(data, x2, y1, tmp2)
i believe problem way treat z1
in testfunc2
. apply
passes each row of z1
z1
of testfunc2
vector, not data.frame. prepare define testfunc2
as:
testfunc2 = function(z1, y1){ d1 = z1[1] x1 = z1[2] res2 = (d1 + 2*x1)^2 + y1 }
r
Comments
Post a Comment