Question

I need to create potentially a huge number of variables. I would like to name them

var.1, var.2, var.3 etc.

I am thinking about using a for loop. To experiment, I use only a single iteration, i.e. i=1. I've tried paste() and print(), but neither works.

paste("detect.","1", sep="") = 2
Error in paste("detect.", "1", sep = "") = 2 : 
target of assignment expands to non-language object

print("detect.","1", sep="") = 1
Error in print("detect.", "1", sep = "") = 1 : 
target of assignment expands to non-language object

I've also tried to add as.vector() and others, but none of them works.

If possible, can anyone provide a better solution without using for loops?

Thanks

Was it helpful?

Solution

You want assign:

> assign(paste("detect.", "1", sep=""), 2)
> detect.1
[1] 2
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top