Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to detect sequential event change properly in R data frame
    primarykey
    data
    text
    <p>I'm a beginer with R. I discover and all theses wonderfull feature to analyse data. I want to filter a data frame by detecting event change. For example, if we took the following data :</p> <pre><code>testcase date event 1 TESTCASE1 2013-06-12 18:12:09 EVENT1 2 TESTCASE1 2013-06-12 18:12:12 EVENT1 3 TESTCASE1 2013-06-12 18:12:15 EVENT2 4 TESTCASE1 2013-06-12 18:12:16 EVENT2 5 TESTCASE1 2013-06-12 18:12:25 EVENT1 6 TESTCASE2 2013-06-12 18:12:10 EVENT4 7 TESTCASE2 2013-06-12 18:12:16 EVENT4 8 TESTCASE2 2013-06-12 18:12:17 EVENT2 9 TESTCASE2 2013-06-12 18:12:26 EVENT2 10 TESTCASE2 2013-06-12 18:12:30 EVENT1 </code></pre> <p>I'd like to return only the line where there are an event changed. For this example, it give this : </p> <pre><code> testcase date event 2 TESTCASE1 2013-06-12 18:12:12 EVENT1 3 TESTCASE1 2013-06-12 18:12:15 EVENT2 4 TESTCASE1 2013-06-12 18:12:16 EVENT2 5 TESTCASE1 2013-06-12 18:12:25 EVENT1 7 TESTCASE2 2013-06-12 18:12:16 EVENT4 8 TESTCASE2 2013-06-12 18:12:17 EVENT2 9 TESTCASE2 2013-06-12 18:12:26 EVENT2 10 TESTCASE2 2013-06-12 18:12:30 EVENT1 </code></pre> <p>The only way I have found to do this, is to use loop. It give the following code :</p> <pre><code>result &lt;- data.frame( testcase = c("TESTCASE1","TESTCASE1","TESTCASE1","TESTCASE1","TESTCASE1","TESTCASE2","TESTCASE2","TESTCASE2","TESTCASE2","TESTCASE2"), date = c("2013-06-12 18:12:09","2013-06-12 18:12:12","2013-06-12 18:12:15","2013-06-12 18:12:16","2013-06-12 18:12:25","2013-06-12 18:12:10","2013-06-12 18:12:16","2013-06-12 18:12:17","2013-06-12 18:12:26","2013-06-12 18:12:30"), event = c("EVENT1","EVENT1","EVENT2","EVENT2","EVENT1","EVENT4","EVENT4","EVENT2","EVENT2", "EVENT1")) tc &lt;- result[1,"testcase"] currentDate &lt;- result[1,"date"] currentEvent &lt;- result[1,"event"] #index variable de sortieoutput j &lt;- 1 output &lt;- c() for(i in 2:length(result[,1])){ if(tc != result[i,"testcase"]){ tc &lt;- result[i,"testcase"]; currentEvent &lt;- result[i,"event"] }else{ #detection de handhover if(result[i,"event"] != currentEvent){ output[j] &lt;- i-1 output[j+1] &lt;- i j &lt;- j+2 currentEvent &lt;- result[i,"event"] } } } output_data &lt;- result[unique(output),] </code></pre> <p>But in R, loop are not recommended and (very) slow, also my data set is very big. Have you got an idea to use a more R compliant solution ?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload