title: Subsetting a data set inside for loop
body: <p>I'm trying to loop through a string vector called iter in order to subset a data frame in R. I would like to filter that data frame using i iter value in the loop and assign it to a variable using iter value i.</p>
  
  <pre><code>iter&lt;- c(COD1,COD2,COD3)
  
      for ( i in iter) {
        assign(i, subset(out,TestId==paste0(i) &amp; AddId=="Curva_F_Cor"))
      }
  </code></pre>
  
  <p>The output of this command would be 3 variables which are dataframes. I'm interested in a column named mu.spline which is present in these 3 variables:</p>
  
  <pre><code>TestID    mu.spline   lambda.spline
  COD1      0.02        3
  COD1      0.03        4
  COD1      0.01        1
  
  TestID    mu.spline   lambda.spline
  COD2      0.1         8
  COD2      0.25        10
  COD2      0.01        3
  
  TestID    mu.spline   lambda.spline
  COD3      0.12        1
  COD3      0.32        8
  COD3      0.22        3
  </code></pre>
  
  <p>However, If I want to obtain the column named mu.spline and assign it to a new variable from subseted variables an error occurs:</p>
  
  <pre><code>for ( i in iter) {
    assign(i, subset(out,TestId==paste0(i) &amp; AddId=="Curva_F_Cor"))
    assign(paste0(i,".mu"), i[,"mu.spline"])
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>Error in i[, "mu.spline"] : incorrect number of dimensions
  </code></pre>
  
  <p>If i try the following code, the iteration is working:</p>
  
  <pre><code>for ( i in iter) {
    assign(i, subset(out,TestId==paste0(i) &amp; AddId=="Curva_F_Cor"))
    i
    assign(paste0(i,".mu"), "hi")
  }
  </code></pre>
  
  <p>It seems the problem occurs when I try to obtain the column named mu.spline from each variable using i[,"mu.spline"]. I wonder why it fails beacuse i tryed this way getting columns (<code>COD1[,"mu.spline"]</code>) outside a loop and it worked...</p>
  
  <p>**Edit for dput():</p>
  
  <pre><code>structure(list(TestId = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 
  2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 
  3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 6L, 6L, 6L, 7L, 7L, 7L, 8L, 8L, 
  8L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L, 12L, 12L, 12L, 
  13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 16L, 16L, 16L, 17L, 
  17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L), .Label = c("Comb1", 
  "Comb2", "COD1", "COD2", "COD3", "COD4", "COD5", 
  "COD6", "COD7", "COD8", "COD9", "COD10", "COD11", 
  "COD12", "COD13", "COD14", "Pat"), class = "factor"), 
      mu.spline = c(0.156373645710651, 0.128179004733465, 0.133922208832118, 
      0.0968325365246728, 0.112497378553166, 0.108787192266453, 
      0.110192954818258, 0.121005105680758, 0.0980394197157738, 
      0.138420857616108, 0.127789639429687, 0.128560390185466, 
      0.110549423439033, 0.108320566548023, 0.098918312107995, 
      0.0828284492044932, 0.104197889210497, 0.122413067260436, 
      0.100261893863431, 0.0938211089313908, 0.0950013179641027, 
      0.145680825059066, 0.139104408376977, 0.126037019624304, 
      0.126708418382696, 0.129821223842992, 0.136480998324424, 
      0.13593684872676, 0.139066913195263, 0.148222162331793, 0.1063086971118, 
      0.167178433353777, 0.0999504815546864, 0.159110219357191, 
      0.125081233896366, 0.163966026506179, 0.15029944955429, 0.116975580695436, 
      0.15276496804095, 0.155339014181045, 0.112171217970295, 0.120104234834245, 
      0.133373734309075, 0.175784287024805, 0.133626401899954, 
      0.140297143337283, 0.0863206151811713, 0.170070971923806, 
      0.152896880973888, 0.10553437562759, 0.124122727198564, 0.163571762302165, 
      0.151047108367937, 0.131416085292366, 0.152515440225195, 
      0.139308623745812, 0.146009754853497, 0.170825235429307, 
      0.147466868348918, 0.126623691613807, 0.147114348605148, 
      0.141084369853073, 0.153670399861141, 0.162948873362462, 
      0.131121302899353, 0.146421599771427, 0.135166111999851, 
      0.157495164357944, 0.126927329131488, 0.159831796004744, 
      0.146936913846553, 0.12183336770971, 0.136669798817364, 0.152333836640196, 
      0.138055091325892)), .Names = c("TestId", "mu.spline"), row.names = c("76", 
  "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", 
  "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", 
  "99", "100", "101", "102", "103", "104", "105", "106", "107", 
  "108", "109", "110", "111", "112", "113", "114", "115", "116", 
  "117", "118", "119", "120", "121", "122", "123", "124", "125", 
  "126", "127", "128", "129", "130", "131", "132", "133", "134", 
  "135", "136", "137", "138", "139", "140", "141", "142", "143", 
  "144", "145", "146", "147", "148", "149", "150"), class = "data.frame")
  </code></pre>