I need to make loop that assigns new values to certain vectors based on vector of vectors names. Overall, it can be easily done just by using assign
. Here is an example:
set.seed(11)
my_future_vectors=c("vec1","vec2","vec3")
for (i in 1:length(my_future_vectors)){assign(my_future_vectors[i],runif(3,0:1))}
The problem with assign
is that whenever I need to add new values to those vectors it overwrites them. If I want add 1
and I do:
assign(my_future_vectors[1],1)
Than:
> vec1
[1] 1
This is understandable, but what works for me is vec1[length(vec1)+1]=1
, which gives output:
> vec1
[1] 0.2772497942 1.0000000000 0.0005183129 1.0000000000
Problem arises whenever i try to use function get
to address certain vector:
get(my_future_vectors[1])[length(my_future_vectors[1])+1]=1
I get an error:
Error in get(my_future_vectors[1])[length(my_future_vectors[1])] = 1 :
could not find function "get<-
So I understand that get
doesnt work this way, so I have no idea how make a loop like that. Please provide solution or workaround to avoid this error. Or am I missing something obvious here?