title: R function to produce a year column
body: <p>I am trying to produce a function that puts a year column in a dataframe.</p>
  
  <p>I can do this without the function by using:</p>
  
  <pre><code>#Create a dataframe with some dates in it. 
  y&lt;-data.frame(Date=(sample(seq(as.Date('2010/01/01'),as.Date('2018/01/01'), by="day"), 20)))      
  
  #create the year column
  library(lubridate)
  y$Year&lt;-year(y$Date)
  </code></pre>
  
  <p>When I try to wrap this inside a function, nothing happens:</p>
  
  <pre><code>#Create a dataframe with some dates
  x&lt;-data.frame(Date=(sample(seq(as.Date('2010/01/01'), as.Date('2018/01/01'), by="day"), 20)))
  
  #Create the function
  yearfunction&lt;-function(dataframe, datecolumn)
  {dataframe$Year&lt;- year(dataframe[,datecolumn])
  }
  
  #Call the function
  yearfunction(x, "Date")
  </code></pre>
  
  <p>What I want to happen is to get the same results as in the first example. Can anyone help with how I can amend the function to achieve this?</p>