title: Convert any number of vectors into a dataframe whilst preserving data types and using vector names as column names in R
body: <p>Is there a simple function (preferably user-written, or found in base R) that takes any number of vectors, and produces a dataframe whist retaining the vectors' data types, and using the vector variables' names as the column names? </p>
  
  <h3>An example</h3>
  
  <p>Inputs (vectors)</p>
  
  <pre><code>&gt; var_a # character
  [1] "a" "b" "c"
  
  &gt; var_b # numeric
  [1] 1 3 4
  
  &gt; var_c # factor
  [1] red   black black
  Levels: black red
  </code></pre>
  
  <p>Desired output</p>
  
  <pre><code>  var_a var_b var_c
  1     a     1   red
  2     b     3 black
  3     c     4 black
  </code></pre>
  
  <p>where the classes are</p>
  
  <pre><code>sapply(my_dataframe, class)
  
  #      var_a       var_b       var_c 
  #"character"   "numeric"    "factor"
  </code></pre>
  
  <h3>Attempt 1 - Using <code>cbind</code></h3>
  
  <p>Using <code>cbind</code> will produce a matrix (with a single data type) - so this method does not maintain the vectors' original data types (it changes all columns to character)</p>
  
  <pre class="lang-r prettyprint-override"><code>first_method &lt;- cbind(var_a, var_b, var_c)
  </code></pre>
  
  <h3>Attempt 2 - Using <code>do.call</code> (similar to <a href="https://stackoverflow.com/questions/24988483/combine-vectors-inside-of-a-list-to-create-a-dataframe-r">here</a>)</h3>
  
  <p>In this case the data types are lost and so are the names of the vector variables</p>
  
  <pre><code>ls &lt;- list(var_a, var_b, var_c)
  second_method &lt;- data.frame(do.call(cbind, ls))
  second_method %&gt;% sapply(class)
  #       X1       X2       X3 
  # "factor" "factor" "factor"
  </code></pre>
  
  <h3>Attempt 3 - using <code>data.frame</code></h3>
  
  <p>This method gets close (it retains the vector names as column names in the dataframe), but unfortunately it converts character data types into factors</p>
  
  <pre class="lang-r prettyprint-override"><code>third_method &lt;- data.frame(var_a, var_b, var_c)
  third_method %&gt;% sapply(class)
  #    var_a     var_b     var_c 
  # "factor" "numeric"  "factor" 
  </code></pre>
  
  <h3>Attempt 4 - Manually declaring each column of the dataframe, AND its name, AND its datatype</h3>
  
  <p>This returns the desired output, however, it is not eloquent, instead taking a <strong><em>lot</em></strong> of manual coding for large numbers of vectors, and is prone to user error because the user must specify the datatype manually for each column</p>
  
  <pre class="lang-r prettyprint-override"><code>fourth_method &lt;- data.frame("var_a"=as.character(var_a), "var_b"=as.numeric(var_b), "var_c"=as.factor(var_c), stringsAsFactors = FALSE)
  fourth_method %&gt;% sapply(class)
  
  #      var_a       var_b       var_c 
  #"character"   "numeric"    "factor" 
  
  </code></pre>
  
  <p>Note: <a href="https://stackoverflow.com/questions/32173557/how-to-transform-a-vector-into-data-frame-with-fixed-dimension">this</a>, <a href="https://stackoverflow.com/questions/12787551/creating-a-data-frame-from-two-vectors-using-cbind">this</a>, and <a href="https://stackoverflow.com/questions/43662457/convert-list-of-vectors-to-data-frame">this</a> solution are unsuitable as they result in loss of data type</p>
  
  <p>Also note: The vectors in this question are not named vectors as referred to in <a href="https://stackoverflow.com/questions/16816032/convert-named-character-vector-to-data-frame">this</a> question</p>
  
  <p>At this point, I am running low on ideas and am unsure what to try next?</p>