title: Why does as.matrix add extra spaces when converting numeric to character?
body: <p>If you use apply over rows on a data.frame with character and numeric columns, apply uses as.matrix internally to convert the data.frame to only characters. But if the numeric column consists of numbers of different lengths, as.matrix adds spaces to match the highest/"longest" number.</p>
  
  <p>An example:</p>
  
  <pre><code>df &lt;- data.frame(id1=c(rep("a",3)),id2=c(100,90,8), stringsAsFactors = FALSE) 
  df
  ##   id1 id2
  ## 1   a 100
  ## 2   a  90
  ## 3   a   8
  as.matrix(df)
  ##      id1 id2  
  ## [1,] "a" "100"
  ## [2,] "a" " 90"
  ## [3,] "a" "  8"
  </code></pre>
  
  <p>I would have expected the result to be:</p>
  
  <pre><code>     id1 id2  
  [1,] "a" "100"
  [2,] "a" "90"
  [3,] "a" "8"
  </code></pre>
  
  <p>Why the extra spaces?</p>
  
  <p>They can create unexpected results when using apply on a data.frame:</p>
  
  <pre><code>myfunc &lt;- function(row){
    paste(row[1], row[2], sep = "")
  }
  &gt; apply(df, 1, myfunc)
  [1] "a100" "a 90" "a  8"
  &gt; 
  </code></pre>
  
  <p>While looping gives the expected result.</p>
  
  <pre><code>&gt; for (i in 1:nrow(df)){
    print(myfunc(df[i,]))
  }
  [1] "a100"
  [1] "a90"
  [1] "a8"
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&gt; paste(df[,1], df[,2], sep = "")
  [1] "a100" "a90"  "a8"  
  </code></pre>
  
  <p>Are there any situations where the extra spaces that are added with as.matrix is useful?</p>