I am trying to create a set of new vectors based on a rule, for a list of vectors. My input consists of 3 normal vectors (index
, rfree
, ret
) and a list of several vectors (roll
), all vectors being the same length. I want the new vectors to follow the rule: if index>roll -> ret, else rfree, so that the index is evaluated against the "k" number of roll vectors giving "k" new vectors that only consist of ret and rfree inputs. My current program doesn't work and I can't figure out why. The error message I get is
"Error in `*tmp*`[[j]] : subscript out of bounds"
But I can't really figure out why. Any help is greatly appreciated.
#Input:
roll <- list(runif(85),runif(85))
index <- runif(85)
rfree <- rnorm(85)
ret <- rnorm(85)
#Programe:
aret <- function(index, roll, ret, rfree, k=2){
aret <- list()
for (j in seq(k))
for (i in 1:length(ret)){
if (roll[[j]][i]>index[i])(aret[[j]][i] <- ret[i])
else(aret[[j]][i] <- rfree[i])
}
}