-2

I am using R to create a variable which can increment after certain operations. i.e., I have a variable called node and I want to write a function to increment it like node1, node2, node3..etc., each time the function is called.

Is this possible? Please help

  • You might have an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Is it possible to step back and give a wider explanation? There might be a better route than writing functions that affect environmental variables. – Pierre L Mar 15 '17 at 00:47
  • Have you tried using the <<- operator? – Sun Bee Mar 15 '17 at 01:25
  • Possible duplicate of [Change variable name in for loop using R](http://stackoverflow.com/questions/16566799/change-variable-name-in-for-loop-using-r) – lbusett Mar 15 '17 at 07:26

1 Answers1

0

I would use a list in your case node <- list(). You can now use the increment to save the your value to the "position" in the list like node[[i]] <- 'my value'. Just increment i like you wanted to do with the variable name. Access the value by node[[1]] etc.

drmariod
  • 11,106
  • 16
  • 64
  • 110