title: Converting a vector into a matrix (in R)
body: <p>I want to create a "n x 8" Matrix from a "n x 1" vector</p>
  
  <p>-- Question: Why do I want to do this?</p>
  
  <p>-- Answer: In order to matrix multiply this against an "8 x 8" markov chain probability transition matrix, and return an "n x 8" Matrix of the predicted states</p>
  
  <p>-- <strong>Solution: I have solved this in Attempt 3 below - but want to know if there is a better way to resolve this (rather than using two transpose functions)?</strong></p>
  
  <hr>
  
  <p><strong>R code</strong></p>
  
  <p>Create a dummy "n x 1" vector: (here we use n = 2)</p>
  
  <pre><code>&gt; temp_vector &lt;- c("state 4", "state 7")
  &gt; temp_vector
  [1] "state 4" "state 7"
  </code></pre>
  
  <p>Expected Output:</p>
  
  <pre><code>NA NA NA TRUE NA NA NA NA
  NA NA NA NA NA NA TRUE NA
  </code></pre>
  
  <hr>
  
  <p>Attempt 1: Convert to matrix:</p>
  
  <pre><code>&gt; temp_matrix &lt;- matrix(temp_vector, 
                  ncol = 8, # there are 8 states
                  nrow = length(temp_vector) # there are 10 rows in the vector
                  )
  &gt; temp_matrix
       [,1]      [,2]      [,3]      [,4]      [,5]      [,6]      [,7]      [,8]     
  [1,] "state 4" "state 4" "state 4" "state 4" "state 4" "state 4" "state 4" "state 4"
  [2,] "state 7" "state 7" "state 7" "state 7" "state 7" "state 7" "state 7" "state 7"
  </code></pre>
  
  <p>Attempt 1 FAIL: This is not ideal, I want a matrix with ONE entry per row, not EIGHT.</p>
  
  <hr>
  
  <p>Attempt 2: Compare the stateSpace above with the matrix, to give a matrix made up of TRUE/FALSE:</p>
  
  <pre><code>&gt; stateSpace &lt;- c("state 1", "state 2", "state 3", "state 4", "state 5", "state 6", "state 7", "state 8")
  
  &gt; temp_matrix == stateSpace
       state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8
  [1,]   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE
  [2,]   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE
  </code></pre>
  
  <p>Attempt 2 FAIL: expected each row to have one TRUE and the rest FALSE</p>
  
  <p>Reason: (I THINK) matrices are compared column-wise.</p>
  
  <hr>
  
  <p>Looking into Attempt 2 further, on an element by element level <strong>this works</strong>:</p>
  
  <pre><code>&gt; temp_matrix[1,1] == colnames(temp_matrix)[1]
  state 1 
    FALSE 
  &gt; temp_matrix[1,2] == colnames(temp_matrix)[2]
  state 2 
    FALSE 
  &gt; temp_matrix[1,3] == colnames(temp_matrix)[3]
  state 3 
    FALSE 
  &gt; temp_matrix[1,4] == colnames(temp_matrix)[4]
  state 4 
     TRUE 
  </code></pre>
  
  <p>Looking into Attempt 2 further, on a row by row level <strong>this works</strong>:</p>
  
  <pre><code>&gt; temp_matrix[1,] == colnames(temp_matrix)[]
  state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8 
    FALSE   FALSE   FALSE    TRUE   FALSE   FALSE   FALSE   FALSE 
  
  &gt; temp_matrix[2,] == colnames(temp_matrix)[]
  state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8 
    FALSE   FALSE   FALSE   FALSE   FALSE   FALSE    TRUE   FALSE 
  </code></pre>
  
  <hr>
  
  <p>Attempt 3: after noting the above learnings of column wise comparison in R</p>
  
  <pre><code>&gt; t(stateSpace == t(temp_matrix))
       state 1 state 2 state 3 state 4 state 5 state 6 state 7 state 8
  [1,]    TRUE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE
  [2,]   FALSE    TRUE   FALSE   FALSE   FALSE   FALSE   FALSE   FALSE
  </code></pre>
  
  <p>Attempt 3 SUCCESS: created this stackoverflow post to see if there is a better way to resolve this (rather than using two transpose functions)</p>
  
  <hr>
  
  <p>Other options: dcast, reshape, spread; sadly did NOT work either.</p>
  
  <p>I tried reshape():</p>
  
  <pre><code>reshape(temp_vector, direction = "wide")
  &gt; Error in data[, timevar] : incorrect number of dimensions
  </code></pre>
  
  <p>I tried spread():</p>
  
  <pre><code>library(tidyr)
  spread(temp_vector, key = numbers, value = value)
  &gt; Error in UseMethod("spread_") : 
    no applicable method for 'spread_' applied to an object of class "factor"
  </code></pre>