Let's say we have 4 vectors, each one of them corresponding to values of some indicator in year i (i is between 11 and 14):
vector_11 <- c(1,2,3,4)
vector_12 <- c(5,6,7,8)
vector_13 <- c(9,10,11,12)
vector_14 <- c(13,14,15,16)
... and the following function :
myfunction <- function (vect){
res <- sum(vect)
return(res)
}
I'd like to "select" the vector according to the value of another variable : year. And then, apply myfunction() to the corresponding vector.
I've tried to do this with a loop, and the function paste() but the problem is that R reads it as a character argument :
year <- 14
for (i in 11:14){
if (year==i){
vect <- myfunction(paste("vector_",i,sep=''))
}
}
sum(get(paste0("vector_",year)))