Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If I understand you correctly, you need something like this:</p> <pre><code>elist &lt;- lapply(seq_len(nrow(test)), function(i) { x &lt;- as.character(test[i,]) x &lt;- unique(na.omit(x)) x &lt;- rep(x, each=2) x &lt;- x[-1] x &lt;- x[-length(x)] r &lt;- matrix(x, ncol=2, byrow=TRUE) if (nrow(r) &gt; 0) { r &lt;- cbind(r, i) } else { r &lt;- cbind(r, numeric()) } r }) do.call(rbind, elist) # i # [1,] "freshman" "junior" "1" # [2,] "junior" "senior" "1" # [3,] "freshman" "junior" "2" # [4,] "junior" "sophomore" "2" # [5,] "sophomore" "senior" "2" # [6,] "freshman" "junior" "3" # [7,] "junior" "sophomore" "3" # [8,] "sophomore" "senior" "3" # [9,] "sophomore" "senior" "4" #[10,] "sophomore" "senior" "5" </code></pre> <p>It is not the most efficient solution, but I think it is fairly didactic. We create edges separately for each row of your input matrix, hence the <code>lapply</code>. To create the edges from a row, we first remove NAs and duplicates, and then include each vertex twice. Finally, we remove the first and last vertex. This way we created an edge list matrix, we only need to drop the first and last vertex and format it in two columns (actually it would be more efficient to leave it as a vector, never mind).</p> <p>When adding the extra column, we must be careful to check whether our edge list matrix has zero rows.</p> <p>The <code>do.call</code> function will just glue everything together. The result is a matrix, which you can convert to a data frame if you like, via <code>as.data.frame()</code>, and then you can also convert the third column to numeric. You can also change the column names if you like.</p>
    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. 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