title: How to Create Dynamic Variable Names in a For Loop in R
body: <p>I have a dataset/table (called <strong>behavioural</strong>) with data from <strong>24</strong> participants - these are named in the format: 's1' to  's24'.</p>
  
  <p>The first 6 rows in the table/dataset:</p>
  
  <pre><code>head(behavioural)[c(1,17)]
     subj     recognition_order
  1   s1                 2
  2   s1                 6
  3   s1                 7
  4   s1                 8
  5   s1                 9
  6   s1                10
  </code></pre>
  
  <p>I want to create a subset for <strong>each</strong> participant and order each of these subsets by the variable <code>recognition_order</code></p>
  
  <p>I have created a loop to do this:</p>
  
  <pre><code>behavioural &lt;- read.table("*my file path*\behavioral.txt", header = TRUE)
  
  subj_counter &lt;- 1
  
  
  for(i in 1:24) {
  subject &lt;- paste("s", subj_counter, sep = "")
  subset_name &lt;- paste(subject, "_subset", sep="")
  
  [subset_name] &lt;- behavioural[which(behavioural$subj  == subject), ]
  [subset_name] &lt;- subset_name[order(subset_name$recognition_order),]
  
  subj_counter = subj_counter + 1
  
  print(subset_name)
  print(subj_counter)
  }
  </code></pre>
  
  <p>And I'm pretty sure the logic is solid, except when I run the loop, it does <strong>not</strong> create 24 subsets. It just creates 1 - <code>s24_subset</code>.</p>
  
  <p><strong>What do I need to do to the bit before "&lt;-" in these 2 lines of code?</strong> </p>
  
  <pre><code>[subset_name] &lt;- behavioural[which(behavioural$subj  == subject), ]
  [subset_name] &lt;- subset_name[order(subset_name$recognition_order),]
  </code></pre>
  
  <p>Because <code>[subset_name]</code> isn't working.</p>
  
  <p>I want the <code>[subset_name]</code> to be dynamic - i.e. each time the loop runs, its value changes and it creates a new subset/variable each time.</p>
  
  <p>I have seen things online about the <code>assign()</code> function but I'm not quite sure how to implement this into my loop?</p>
  
  <p>Thank you in advance!</p>