Extract rows for the first occurrence of a variable in a group
<p>I have a huge dataset (more than 2 million rows of over 100 variables; below is a small sample). For each <code>subj_trial</code> group, I want to find the first occurrence of each unique variable containing in ".wav" in <code>message</code>. It should be just containing, not ending (i.e. *.wav), because some rows have a bunch of information in the <code>message</code> fields (not pictured in the example, sorry). </p>

<p>It would be OK to output a data.frame that only had those three columns, but it's not necessary. I will later need to use the <code>timestamp</code> column for analyses.</p>

<p>I've found this question: <a href="https://stackoverflow.com/questions/19944334/extract-rows-for-the-first-occurrence-of-a-variable-in-a-data-frame?noredirect=1&amp;lq=1">Extract rows for the first occurrence of a variable in a data frame</a>, but for the life of me I cannot work that example to fit mine. </p>

<p>Here's some sample data:</p>

<pre><code>   subj_trial     message timestamp
1         1_1 message 459    755616
2         1_1           .    755618
3         1_1   test1.wav    755662
4         1_1           .    765712
5         1_1   test1.wav    767918
6         1_2           .    769342
7         1_2   test2.wav    775662
8         1_2           .    786412
9         1_2   test2.wav    797460
10        1_2           .    807626
11        1_3   test3.wav    817794
12        1_3  warning 11    827960
13        2_1 message 481    817313
14        2_1   test1.wav    817347
15        2_1           .    834959
16        2_1   test1.wav    855007
17        2_1           .    880107
18        2_2           .    895723
19        2_2   test2.wav    922671
20        2_2           .    958003
21        2_2   test2.wav    994385
22        2_3           .   1016217
23        2_3   test3.wav   1036899
24        2_3           .   1047331
25        2_3   test3.wav   1142527
</code></pre>

<p>This is a very small example of what I'm dealing with, here. For each <code>subj_trial</code> group there are probably 3000 lines, and there are over 700 groups. </p>

<p>Here's an example of what I'd like to have. </p>

<pre><code>  subj_trial   message timestamp
1        1_1 test1.wav    755662
2        1_2 test2.wav    775662
3        1_3 test3.wav    817794
4        2_1 test1.wav    817347
5        2_2 test2.wav    922671
6        2_3 test3.wav   1036899
</code></pre>

<p>I've figured out how to get the unique values in <code>message</code> over the entire dataset by doing this:</p>

<pre><code>unique_message &lt;- df[match(unique(df$message), df$message),]
</code></pre>

<p>But I can't figure out how to do it by group. I've also tried using <code>group_by</code> in the <code>dplyr</code> package but can't get that to work, either. Have mercy and show me the way, friends. Thanks!</p>