I have a 1000 x 3
matrix of combinations of the integers from 1:10
(e.g. each column is an index ranging from 1 to 10 and I want to look at combinations of indices). I would like to get the row index of the combination that results in a partial row sum satisfying some condition.
cvec = c(14,15)
L <- 3
vec <- seq(10)
lst <- lapply(numeric(L), function(x) vec)
mat = as.matrix(expand.grid(lst))
partial = NULL
for (k in seq(L-1)){
for (r in seq(L-k+1)){
partial = c(which(rowSums(mat[,r:(r+k-1)])<=cvec[k]),partial)
}
}
I keep getting the following error:
Error in rowSums(mat[, r:(r + k - 1)]) :
'x' must be an array of at least two dimensions
However, when I run which(rowSums(mat[,r:(r+k-1)])<=cvec[k])
or even just rowSums(mat[,r:(r+k-1)])
outside the for loops, I don't get an error. is there a reason for this?