Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract year from date

Tags:

r

How can I remove the first elements from a variable, especially if this variable has a special characters. For example, I have the following column:

Date 01/01/2009 01/01/2010 01/01/2011 01/01/2012 

I need to have a new column like the following:

Date 2009 2010 2011 2012 
like image 788
hbtf.1046 Avatar asked Apr 12 '16 08:04

hbtf.1046


2 Answers

As discussed in the comments, this can be achieved by converting the entry into Date format and extracting the year, for instance like this:

format(as.Date(df1$Date, format="%d/%m/%Y"),"%Y") 
like image 76
RHertel Avatar answered Sep 29 '22 16:09

RHertel


library(lubridate) a=mdy(b) year(a) 

https://cran.r-project.org/web/packages/lubridate/vignettes/lubridate.html http://vita.had.co.nz/papers/lubridate.pdf

like image 42
Ajay Ohri Avatar answered Sep 29 '22 16:09

Ajay Ohri