title: Loop through dataframe in R and measure time difference between two values
body: <p><strong>Summary:</strong> I am analyzing the time difference between an occured stimuli (A&amp;B) and a possible response of the user.</p>
  
  <p>The dataset has the following structure:</p>
  
  <pre><code>    structure(list(User = c("005b98f3-5b1b-4d10-bdea-a55d012b2844",
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844", "005b98f3-5b1b-4d10-bdea-a55d012b2844", 
  "005b98f3-5b1b-4d10-bdea-a55d012b2844"), Date = c("25.11.2015 13:59", 
  "03.12.2015 09:32", "07.12.2015 08:18", "08.12.2015 19:40", "08.12.2015 19:40", 
  "22.12.2015 08:52", "22.12.2015 08:50", "22.12.2015 15:42", "22.12.2015 20:46", 
  "05.01.2016 11:33", "05.01.2016 11:35", "05.01.2016 13:22", "05.01.2016 13:21", 
  "05.01.2016 13:22", "06.01.2016 09:18", "14.02.2016 22:47", "20.02.2016 21:27", 
  "01.04.2016 13:52", "24.07.2016 07:03", "04.08.2016 08:25"), 
      Hour = c(1645L, 1833L, 1928L, 1963L, 1963L, 2288L, 2288L, 
      2295L, 2300L, 2627L, 2627L, 2629L, 2629L, 2629L, 2649L, 3598L, 
      3741L, 4717L, 7447L, 7712L), StimuliA = c(1L, 0L, 1L, 0L, 
      0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 
      0L), StimuliB = c(0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 
      1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L), Responses = c(0L, 
      1L, 0L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 1L, 1L, 1L, 0L, 1L, 
      1L, 1L, 1L, 0L)), .Names = c("User", "Date", "Hour", "StimuliA", 
  "StimuliB", "Responses"), row.names = c(NA, -20L), class = c("tbl_df", 
  "tbl", "data.frame"))
  </code></pre>
  
  <p>Additional Information on the data: Every row in the datatable is an event log where a User either perceived a certain Stimuli or performed an action (Response). Hour: The "Hour" since the start of the project, when the event occured.</p>
  
  <p><strong>Goal:</strong> The overall goal is to measure the time between an the stimuli and the response. (if there was one) I would like to create a loop which goes through the dataset for every User and if the value of a Stimuli is 1, it checks whether later there is a response of the user and the creates a vector with the values for A and one for B.</p>
  
  <p><strong>Question:</strong>
  Would i do this with a for loop, which goes through every User and checks the perceived Stimuli and if there is the value 1 checks whether the same User ID has the value 1 in the closest Response and then compares the 2 dates?</p>
  
  <p><em>Subquestions // Things I am struggeling with</em></p>
  
  <ol>
  <li>How do i actually loop through every row and check it for the conditional statement and if TRUE execute a command? (ifelse?). </li>
  <li>How would i then as a command save the value of an other cell in this row?</li>
  <li>To then tell R to look for the closest Response of the same User ID (chronological) and calculate the time difference between those 2 values?</li>
  <li>For finally creating a vector with those calculated values</li>
  </ol>
  
  <p>Desired result:</p>
  
  <pre><code>Stimuli A c=(11253, 2122, 56969), Stimuli B c=(19512,107)
  </code></pre>
  
  <p>My own code i produced so far is not very helpful. I was experimenting with for loops and if statements, but also the ifelse function.</p>
  
  <p>I am a newbie with R, but did multiple classes on datacamp, but still I am struggling to apply it to my own work of my master thesis. Thanks for all the help.</p>
  
  <p>Additional Info:</p>
  
  <pre><code>R version 3.4.0 (2017-04-21)
  Platform: x86_64-w64-mingw32/x64 (64-bit)
  Running under: Windows &gt;= 8 x64 (build 9200)
  </code></pre>