Akshoya Roy
Akshoya Roy

Reputation: 1

How can I specify data structure name from the string output in r programming?

I am coding this in r and solved this in an alternative way to make the vector to a list and assign value to each of the element of the list, but is there any other direct simple approach?

for(i in 1:5){
paste('var',i,sep='')=i
}

i want output where 1:5 will assign like

var1=1
var2=2
var3=3
var4=4
var5=5

Upvotes: 0

Views: 24

Answers (1)

Konrad Rudolph
Konrad Rudolph

Reputation: 545905

Don’t do this. Use a vector or list instead:

var = 1 : 5

Now you can use var[1] (instead of var1) etc.

Your code doesn’t work because paste creates a character vector, not a variable name.

Upvotes: 1

Related Questions