generating sequences using list of "from" values
I have a list of starting indices as follows:
values <- matrix (1:25, nrow=5)
indexMatrix <- matrix (seq_along (values) , nrow=nrow(values))
fWidth <- 3
startIndices<- list (
vert=indexMatrix [1:(nrow (values)-fWidth) , 1:ncol (values)],
horiz=indexMatrix [1:nrow (values) , 1:(ncol (values)-fWidth)] ,
upDiag=indexMatrix [fWidth:(nrow (values) - fWidth) , 1:(ncol (values) -
fWidth) ] ,
dnDiag=indexMatrix [1: (nrow (values) - fWidth) , 1: (ncol (values) -
fWidth) ]
)
I am trying to generate sequences using these subsets as starting points
using this code:
funs<-c(1 , nrow (values) , nrow (values) - 1 , nrow (values) + 1
)
#browser()
seqs<- for (i in seq_along (startIndices)){
unlist (
lapply (startIndices [i] ,
seq , by=funs[i] , to=fWidth*funs[i]) ,
FALSE , FALSE
)
}
This code results in the error message, Error in seq.default(X[[1L]], ...)
: 'from' must be of length 1. I seem to have some fundamental
misunderstanding of how the lapply function works. I had thought that it
would use my the initial dataset as the list of arguments for the provided
function, but it doesn't appear to be using the just the list items, but
the entire list. This is part of a larger function to calculate the
product of all n adjacent values in a matrix (n=width).
Apologies if I've left anything out; this is my first question here.
edit
Here is the complete function as I've written it so far, since I want to
make sure I don't leave anything important out:
adjProd<- function (values , width) {
fWidth<- width-1
indexMatrix <- matrix (seq_along (values) , nrow=nrow(values))
startIndices<- list (
vert=indexMatrix [1:(nrow (values)-fWidth) , 1:ncol (values)],
horiz=indexMatrix [1:nrow (values) , 1:(ncol (values)-fWidth)] ,
upDiag=indexMatrix [fWidth:(nrow (values) - fWidth) , 1:(ncol
(values) - fWidth) ] ,
dnDiag=indexMatrix [1: (nrow (values) - fWidth) , 1: (ncol
(values) - fWidth) ]
)
funs<-c(1 , nrow (values) , nrow (values) - 1 , nrow (values) + 1
)
browser()
seqs<- for (i in seq_along (startIndices)){
unlist (
lapply (startIndices [i] ,
seq , by=funs[i] , to=fWidth*funs[i]) ,
FALSE , FALSE
)
}
seqs <- values [seqs]
max (
sapply (seqs , prod)
)
}
No comments:
Post a Comment