Note that there are some explanatory texts on larger screens.

plurals
  1. POSubstract last N values from rle() object
    primarykey
    data
    text
    <p>The following function is used to create a path overview for the following dataset:</p> <pre><code>tc &lt;- textConnection(' path touchpoint time abc A 1 abc A 2 abc B 3 abc C 4 def A 2 def B 3 def D 4 def C 5 def D 6 ghi A 1 ghi A 2 ghi A 3 ghi C 4 jkl A 5 jkl A 6 jkl B 7 jkl C 8 mno B 1 mno A 2 mno A 3 mno C 4 pqr A 1 pqr C 2 ') paths &lt;- read.table(tc, header=TRUE) </code></pre> <p>--</p> <pre><code>library(plyr) foo &lt;- function(x){ r &lt;- rle(as.character(x)) short &lt;- paste0(r$values, collapse="_") long &lt;- paste0(r$values, "(", r$lengths, ")", collapse="_") data.frame(short, long) } ddply(paths, .(path), function(x)foo(x$touchpoint)) path short long 1 abc A_B_C A(2)_B(1)_C(1) 2 def A_B_D_C_D A(1)_B(1)_D(1)_C(1)_D(1) 3 ghi A_C A(3)_C(1) 4 jkl A_B_C A(2)_B(1)_C(1) 5 mno B_A_C B(1)_A(2)_C(1) 6 pqr A_C A(1)_C(1) </code></pre> <p>Thus this function creates two forms of 'paths':</p> <ul> <li>Short provides the sequence of touchpoints per path from least recent to most recent. </li> <li>Long provides the sequence of touchpoints per path from least recent to most recent including the number of times a touchpoint was involved. </li> </ul> <p>Since the number of touchpoints can be quite large for some paths, I would like to incorporate the following constraint: only select the <code>n</code> most recent values from <code>short</code> and <code>long</code>. Since the paths are constructed from an <code>rle()</code> object, my question is:</p> <p>How can I get <code>N</code> values and their corresponding lengths from an rle() object? Since the paths are saved from least recent touchpoint to most recent touchpoint, the last <code>N</code> values and corresponding lengths need to be selected. <code>rle()</code> documentation does not provide a solution for this issue.</p> <p>Expected outcome if <code>N=2</code> will be:</p> <pre><code> path short long 1 abc B_C B(1)_C(1) 2 def C_D C(1)_D(1) 3 ghi A_C A(3)_C(1) 4 jkl B_C B(1)_C(1) 5 mno A_C A(2)_C(1) 6 pqr A_C A(1)_C(1) </code></pre>
    singulars
    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