title: How to rewrite loops properly?
body: <p>thematically I would like to convert some data into <code>XML</code>. Thanks to some answers on SO I found a proper solution to that issue. However, I would like to change the syntax in order to be able to process large <code>.csv</code> files.</p>
  
  <p>Here some data:</p>
  
  <pre><code># Some data
  df &lt;-
    read.csv(textConnection('"ID","Name","City","Age"
  "1","Steve","Boston",33
  "2","Michael","Dallas",45
  "3","John","New York",89
  "4","Thomas","LA",62
  "5","Clint","Paris",30'),
     as.is=TRUE)
  
   head(df)
      ID    Name     City Age
   #1  1   Steve   Boston  33
   #2  2 Michael   Dallas  45
   #3  3    John New York  89
   #4  4  Thomas       LA  62
   #5  5   Clint    Paris  30
  
  # To create a XML file
  library(XML)
  
  # For each employee
  # Measure the execution
  start.time &lt;- Sys.time()
  
  xml &lt;- xmlTree()
  xml$addTag("Document", close=FALSE)
  
  for (i in 1:nrow(df)) {
    xml$addNode("employee", attrs = c(id = df[i,"ID"]), close = FALSE)
    appNames &lt;- names(df)[names(df) != "ID"]
    for (j in appNames) {
      xml$addNode(j, df[i, j])
    }
    xml$closeTag()
  }
  xml$closeTag()
  
  end.time &lt;- Sys.time()
  time.taken &lt;- end.time - start.time
  time.taken
  
  # Execution performance
  Time difference of 0.0321269 secs
  
  # How the XML looks like
  cat(saveXML(xml))
  &lt;?xml version="1.0"?&gt;
  
  &lt;Document&gt;
    &lt;employee id="1"&gt;
      &lt;Name&gt;Steve&lt;/Name&gt;
      &lt;City&gt;Boston&lt;/City&gt;
      &lt;Age&gt;33&lt;/Age&gt;
    &lt;/employee&gt;
    &lt;employee id="2"&gt;
      &lt;Name&gt;Michael&lt;/Name&gt;
      &lt;City&gt;Dallas&lt;/City&gt;
      &lt;Age&gt;45&lt;/Age&gt;
    &lt;/employee&gt;
    &lt;employee id="3"&gt;
      &lt;Name&gt;John&lt;/Name&gt;
      &lt;City&gt;New York&lt;/City&gt;
      &lt;Age&gt;89&lt;/Age&gt;
    &lt;/employee&gt;
    &lt;employee id="4"&gt;
      &lt;Name&gt;Thomas&lt;/Name&gt;
      &lt;City&gt;LA&lt;/City&gt;
      &lt;Age&gt;62&lt;/Age&gt;
    &lt;/employee&gt;
    &lt;employee id="5"&gt;
      &lt;Name&gt;Clint&lt;/Name&gt;
      &lt;City&gt;Paris&lt;/City&gt;
      &lt;Age&gt;30&lt;/Age&gt;
    &lt;/employee&gt;
  &lt;/Document&gt;
  </code></pre>
  
  <p>Given huge <code>.csv</code> I thought it is appropriate to avoid dataframes and uses lists. However dont know whether that is a correct approach:</p>
  
  <pre><code>df1&lt;- lapply(unique(df[,1]), function(x) df[df[,1] == x,])
  
  df1[1]
  
      ID  Name   City Age
   #1  1 Steve Boston  33
  </code></pre>
  
  <p>Now I would like to change structure of the nested loop and use <code>lappy</code> on sublists:</p>
  
  <pre><code>lapply(df1, function(v)  xml$addNode("employee", attrs = c(ID = df1[[i,"ID"]]), close = FALSE))
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>lapply(df1, function(x) lapply(appNames, function(v) xml$addNode(j, df[i, j])))
  </code></pre>
  
  <p>Not sure how to get to the same <code>XML</code> output using lapply</p>