title: Save lapply output as data frame loop
body: <p>I have the following data in R:</p>
  
  <pre><code>dat &lt;- read.table(text="
  city var value
  a   var1    0.19
  b   var1    0.67
  c   var1    0.19
  a   var2    0.14
  b   var2    0.38
  c   var2    0.27
  a   var3    0.59
  b   var3    0.42
  c   var3    0.27
  a   var4    0.28
  b   var4    0.37
  c   var4    0.91
  ", header=TRUE)
  </code></pre>
  
  <p>I need to create different data frames of city b with the rest of the cities (e.g. city b with city a, city b with city c, etc.). Andrie managed to get that with the <code>lapply</code> function.  </p>
  
  <pre><code>lapply(
    setdiff(levels(dat$city), "b"),
    function(i){
      ret &lt;- dat[dat$city %in% c("b", i), ]
      ret[order(ret$var, ret$city), ]
    })
  </code></pre>
  
  <p>Then I get the output I need, but I do not know how to save each of the [[i]] as a different dataframe. Eg. [[1]] would be citybcitya, [[2]] would be citybcityc, etc.</p>
  
  <pre><code>[[1]]
     city  var value
  2     b var1  0.67
  1     a var1  0.19
  5     b var2  0.38
  4     a var2  0.14
  8     b var3  0.42
  7     a var3  0.59
  11    b var4  0.37
  10    a var4  0.28
  
  [[2]]
     city  var value
  2     b var1  0.67
  3     c var1  0.19
  5     b var2  0.38
  6     c var2  0.27
  8     b var3  0.42
  9     c var3  0.27
  11    b var4  0.37
  12    c var4  0.91
  </code></pre>
  
  <p>How can I achieve it without the for loop?</p>