Reason for the error.
as.symbol(paste0(x,y,z))
creates a symbol from your pasted string.
eval(as.symbol(paste0(x,y,z))
then evaluates the value of that symbol. So at the minute you have created a variable called 'blablabla' with nothing in. You're then evaluating it to give numeric(0)
.
You then return numeric(0)
and try to assign a value to it giving you the error.
Solution to Error
I won't put in the checks and things, but lets say you created a variable called blablabla and assign the value as 1 (blablabla <- 1
). This prevents the previous error occurring. Then use the code:
tr <- function(x,y,z, index, value){
var_temp <- eval(as.symbol(paste0(x,y,z)))
var_temp[index] <- value
return(var_temp)
}
To assign values to different values in the vector depending on the index and value arguments. You'll have to assign the output to something as well, because blablabla will only be changed within the scope of the function, so it would be used: blablabla <- tr("bla", "bla", "bla", 1, 2)
to set the first element to two.
Does that answer the question?