Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here is a simple base R solution (explanation below):</p> <pre><code>spl &lt;- with(df1, strsplit(as.charcter(B), split = "; ", fixed = TRUE)) lens &lt;- sapply(spl, length) out &lt;- with(df1, data.frame(A = rep(A, lens), B = unlist(spl))) </code></pre> <p>Which gives us:</p> <pre><code>R&gt; out A B 1 aaa 111 2 aaa 222 3 bbb 333 4 ccc 444 5 ccc 555 6 ccc 666 7 ccc 777 8 ddd 888 9 ddd 999 </code></pre> <p>What is the code doing? Line 1:</p> <pre><code>spl &lt;- with(df1, strsplit(as.character(B), split = "; ", fixed = TRUE)) </code></pre> <p>breaks apart each of the strings in <code>B</code> using <code>"; "</code> as the characters to split on. We use <code>fixed = TRUE</code> (as suggested by <strong>@Marek</strong> in the comments) to speed up the matching and splitting as in this case we do not need to match using a regular expression, we simply want to match on the stated string. This gives us a list with the various elements split out:</p> <pre><code>R&gt; spl [[1]] [1] "111" "222" [[2]] [1] "333" [[3]] [1] "444" "555" "666" "777" [[4]] [1] "888" "999" </code></pre> <p>The next line simply counts how many elements there are in each component of the list <code>spl</code></p> <pre><code>lens &lt;- sapply(spl, length) </code></pre> <p>which gives us a vectors of lengths:</p> <pre><code>R&gt; lens [1] 2 1 4 2 </code></pre> <p>The final line of the solution plugs the outputs from the two previous steps into a new data frame. The trick is to repeat each element of <code>df1$A</code> <code>lens</code> number of times; for which we use the <code>rep()</code> function. We also need to unwrap the list <code>spl</code> into a vector which we do with <code>unlist()</code>:</p> <pre><code>out &lt;- with(df1, data.frame(A = rep(A, lens), B = unlist(spl))) </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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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