TeBA
TeBA

Reputation: 1

Need to use a value which name is generated automatically

I`m using

assign( paste("ship", b, sep = ""),c())

as a vector where i want to save the coordinates of certain elements of a matrix.

Now i want to use the vector, as a example the first vector it´s named ship1 and i want to add elements to that vector but I can`t use append and paste("ship", b, sep = "") because I get this error

Error in paste("barco", b, sep = "") <- append(paste("barco", b, sep = ""),  : 
target of assignment expands to non-language object

My question is: How can I use my vector without using specifically ship1 thus been able to use a generic method to fill all other "ship b" vectors

Upvotes: 0

Views: 31

Answers (1)

G5W
G5W

Reputation: 37641

You need get.

b = 1
VName = paste("ship", b, sep = "")
assign(VName,c())
assign(VName, append(get(VName), 1:3))
get(VName)
[1] 1 2 3

But see @MauritsEvers comment about using assign

Upvotes: 1

Related Questions