title: How to search for certain elements in a chronological order in text to create simplified dataframe
body: <p><strong>Intro to data</strong></p>
  
  <p>I have a text file document or csv, which contains the coding from a website, where I could not export the data to an excel sheet. That means that the data itself is bunch of code, where elements are differentiated by certain bits of text. </p>
  
  <p><em>Understanding the data</em></p>
  
  <p>Fx. Each varaible starts with <code>&lt;a:FN&gt;</code>. each element in the varaible, x, has a code that starts with <code>&lt;a:DT&gt;...</code> 
  and y has a code that starte with <code>&lt;a:KY&gt;...</code> where the value for this element starts with <code>&lt;a:VL&gt;</code>.
  The coding for each variable, as well as the underlying code related to this variable, starts with <code>&lt;a:PLI&gt;</code>.</p>
  
  <p><strong>Data structure</strong></p>
  
  <pre><code>So the structure of the file is as following: 
  
  
     `&lt;a:PLI&gt;`
        &lt;a:DT&gt;...
         `&lt;a:FN&gt;...` 
            `&lt;a:KY&gt;...`
             &lt;a:VL&gt;...
      `&lt;a:PLI&gt;`
        &lt;a:DT&gt;...
         `&lt;a:FN&gt;...` 
             `&lt;a:KY&gt;...`
              &lt;a:VL&gt;...
  </code></pre>
  
  <p>When imported to R, the dataframe only has 1 column and a total of approx. 35.000 rows. Each bit of code has its own row. </p>
  
  <p><strong>What I wish to do &amp; Expected result</strong></p>
  
  <p>What I want to do, is transform this complicated text file into a new dataframe, containing all the elements that I am interested in. </p>
  
  <p>For example for each group/variable <code>(a&lt;:FN)</code> I want to find X <code>(&lt;a:DT&gt;)</code>, where I then want to find Y <code>(&lt;a:KY&gt;org-key&lt;/a:KY&gt;)</code> and Z <code>(&lt;a:VL)</code> to the org-key. 
  I want to do this for all X's in all groups/varaibles, resulting in an output, where each X is grouped with its respecting Y and Z value. </p>
  
  <p>I am not sure how to solve my question. I have thought that a for-loop might be the way, or using something from the apply-family like sapply. Frankly I am unsure where to even start, which is why I am reaching out. </p>
  
  <p>What I have envisioned to do, is set up a function that describes what it is I want to do: </p>
  
  <pre><code>bespoke &lt;- function (x) {
  
  find x in df      ## X could be one of the varaibles I am looking for
  find y after x    ## Find y for x in df
  find z after x    ## find z for x in df
  return all elements to a df  ## I wnat to repeat this function for all variables in df. 
  }
  
  df2 &lt;- adply(keys, bespoke) 
  </code></pre>
  
  <p>I am not used to working with functions, so I don't really know how to even start, also because I have several underlying critieria for my function that I want to follow.</p>
  
  <p>I hope that someone can help me move forward. </p>
  
  <p><strong>Piece of data:</strong> </p>
  
  <pre><code>    &lt;a:PLI&gt;
         &lt;a:DT&gt;False&lt;/a:DT&gt;
         &lt;a:DTR&gt;False&lt;/a:DTR&gt;
         &lt;a:Desc&gt;text text text&lt;/a:Desc&gt;
         &lt;a:FN&gt;Type&lt;/a:FN&gt;
         &lt;a:PSPS&gt;
            &lt;a:PSP&gt;
               &lt;a:KY&gt;org-group&lt;/a:KY&gt;
               &lt;a:VL&gt;40100&lt;/a:VL&gt;
            &lt;/a:PSP&gt;
            &lt;a:PSP&gt;
               &lt;a:KY&gt;org-key&lt;/a:KY&gt;
               &lt;a:VL&gt;60205&lt;/a:VL&gt;
            &lt;/a:PSP&gt;
            &lt;a:PSP&gt;
               &lt;a:KY&gt;org-systemkey&lt;/a:KY&gt;
               &lt;a:VL&gt;1005&lt;/a:VL&gt;
            &lt;/a:PSP&gt;
            &lt;a:PSP&gt;
               &lt;a:KY&gt;org-report-type&lt;/a:KY&gt;
               &lt;a:VL&gt;text text text&lt;/a:VL&gt;
            &lt;/a:PSP&gt;
         &lt;/a:PSPS&gt;
         &lt;a:TI&gt;80200&lt;/a:TI&gt;
      &lt;/a:PLI&gt;
      &lt;a:PLI&gt;
         &lt;a:DT&gt;Room&lt;/a:DT&gt;
         &lt;a:DTR&gt;Room&lt;/a:DTR&gt;
         &lt;a:Desc/&gt;
         &lt;a:FN&gt;StartRoom&lt;/a:FN&gt;
         &lt;a:PSPS&gt;
            &lt;a:PSP&gt;
               &lt;a:KY&gt;org-group&lt;/a:KY&gt;
               &lt;a:VL&gt;13020&lt;/a:VL&gt;
            &lt;/a:PSP&gt;
            &lt;a:PSP&gt;
               &lt;a:KY&gt;org-key&lt;/a:KY&gt;
               &lt;a:VL&gt;13130&lt;/a:VL&gt;
            &lt;/a:PSP&gt;
         &lt;/a:PSPS&gt;
         &lt;a:TI&gt;12500&lt;/a:TI&gt;
      &lt;/a:PLI&gt;
      &lt;a:PLI&gt;
         &lt;a:DT&gt;Other room&lt;/a:DT&gt;
         &lt;a:DTR&gt;Other room&lt;/a:DTR&gt;
         &lt;a:Desc/&gt;
         &lt;a:FN&gt;StartRoom&lt;/a:FN&gt;
         &lt;a:PSPS&gt;
            &lt;a:PSP&gt;
               &lt;a:KY&gt;org-group&lt;/a:KY&gt;
               &lt;a:VL&gt;11160&lt;/a:VL&gt;
            &lt;/a:PSP&gt;
            &lt;a:PSP&gt;
               &lt;a:KY&gt;org-key&lt;/a:KY&gt;
               &lt;a:VL&gt;15152&lt;/a:VL&gt;
            &lt;/a:PSP&gt;
         &lt;/a:PSPS&gt;
         &lt;a:TI&gt;15552&lt;/a:TI&gt;
      &lt;/a:PLI&gt;
  </code></pre>