0

I have a file with multiple data formats :

date<-structure(list(date = structure(c(10L, 45L, 40L, 24L, 53L, 51L, 
13L, 68L, 29L, 11L, 40L, 47L, 52L, 34L, 39L, 46L, 20L, 15L, 26L, 
72L), .Label = c("", "01/2005", "01/2015", "03/2008", "05/2008", 
"05/2012", "06/2014", "08/2008", "09/2010", "1/12/2001", "1/2002", 
"10/2007", "10/2010", "10/2011", "11/2013", "12/2004", "12/2006", 
"12/2010", "12/2014", "15/02/2014", "16/04/2014", "16/12/2014", 
"17/07/2006", "1968", "1979", "1982", "1984", "1985", "1986", 
"1992", "1993", "1994", "1997", "1998", "1998/02", "2/2008", 
"2/8/2000", "2000", "2001", "2002", "2002/08", "2003", "2004", 
"2005-01-01", "2006-06-01", "2007", "2007-12-20", "2008", "2008-06-28", 
"2009", "2010", "2011", "2012", "2012/03", "2013", "2014", "21/10/2014", 
"23/10/2014", "24/2000", "3/2007", "5/2009", "6/2001", "6/2012", 
"8/5/1993", "9/2009", "April 2012", "Jan 1975", "Jan 1976", "Mar 1976", 
"May 1976", "Nov 18, 1975", "Sep 10, 1976"), class = "factor")), .Names = "date", row.names = c(NA, 
-20L), class = "data.frame")

What I would like to do is extract the year part of these dates.

How can I do this? My idea was to put all dates in a standard format then extract the year with a subsrtring. However, I tried to standardise the dates using as.POSIXctbut given the lack of a standard format this doesn't work.

user2568648
  • 3,001
  • 8
  • 35
  • 52
  • The question is what you want to do with dates without month or/and day. Once that is decided, there should be no problem. – Metrics Feb 19 '15 at 13:56
  • Thanks but `package ‘lubridate’ is not available (for R version 2.15.2)`. As I need to extract just the year, it is not that important to know if "3/2007" is `"%m%Y"`or `"%d%Y"`. – user2568648 Feb 19 '15 at 14:18
  • @Metrics - I would then like to do a count of how many cases occur in each year - so year can be either numeric or character. – user2568648 Feb 19 '15 at 14:20

0 Answers0