title: Concatenate columns and add them to beginning of Data Frame
body: <p>Noob here to R. Trying to figure something out. I need to build a function that adds a new column to the beginning of a dataset. This new column is a concatenation of the values in other columns that the user specifies.</p>
  
  <p>Imagine this is the data set named myDataSet:</p>
  
  <pre><code>col_1    col_2    col_3    col_4
  bat      red      1        a
  cow      orange   2        b
  dog      green    3        c
  </code></pre>
  
  <p>The user could use the function like so:</p>
  
  <pre><code>addPrimaryKey(myDataSet, cols=c(1,3,4))
  </code></pre>
  
  <p>to get the result of a new data set with columns 1, 3 and 4 concatenated into a column called ID and added to the beginning, like so:</p>
  
  <pre><code>ID        col_1    col_2    col_3    col_4
  bat1a     bat      red      1        a
  cow2b     cow      orange   2        b
  dog4c     dog      green    3        c
  </code></pre>
  
  <p>This is the script I have been working on but I have been staring at it so long, I think I have made a few mistakes. I can't figure out how to get the column numbers from the arguments into the paste function properly.</p>
  
  <pre><code>addPrimaryKey &lt;- function(df, cols=NULL){
  
    newVector = rep(NA, length(cols)) ##initialize vector to length of columns
  
    colsN &lt;- as.numeric(cols)
  
    df &lt;- cbind(ID=paste(
      for(i in 1:length(colsN)){
        holder &lt;- df[colsN[i]]
        holder
      }
    , sep=""), df) ##concatenate the selected columns and add as ID column to df
  df
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated. Thanks so much</p>