-
create vector of dates in standard format
my.dates <- as.Date(format(ISOdatetime(2000:2009,1:10,1:5,0,0,0),"%Y-%m-%d"))
my.dates
[1] "2000-01-01" "2001-02-02" "2002-03-03" "2003-04-04" "2004-05-05"
[6] "2005-06-01" "2006-07-02" "2007-08-03" "2008-09-04" "2009-10-05"
(make sure your object is of date fromat, check it with str(your.object)
-
extract years using format:
my.years <- format(my.dates,"%Y") # %y without century
my.years
[1] "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009"
my.months <- format(my.dates,"%m")
my.months
[1] "01" "02" "03" "04" "05" "06" "07" "08" "09" "10"
-
or names of month (in current local)
my.months <- format(my.dates,"%b") # %B for long form
my.months
[1] "Jan" "Feb" "Mär" "Apr" "Mai" "Jun" "Jul" "Aug" "Sep" "Okt"
my.days <- format(my.dates,"%d")
my.days
[1] "01" "02" "03" "04" "05" "01" "02" "03" "04" "05"
-
or week day (in current local)
my.days <- format(my.dates,"%a") # %A for long form, %w (0-6) of %u (1-7) for number
my.days
[1] "Sa" "Fr" "So" "Fr" "Mi" "Mi" "So" "Fr" "Do" "Mo"
my.days <- format(my.dates,"%j")
my.days
[1] "001" "033" "062" "094" "126" "152" "183" "215" "248" "278"
-
or week of year (sunday as first day of the week)
my.weeks <- format(my.dates,"%U") # %W for monday = first day of the week
my.weeks
[1] "00" "04" "09" "13" "18" "22" "27" "30" "35" "40"
my.days <- format(my.dates,"%x")
my.days
[1] "01.01.2000" "02.02.2001" "03.03.2002" "04.04.2003" "05.05.2004"
[6] "01.06.2005" "02.07.2006" "03.08.2007" "04.09.2008" "05.10.2009"
I was looking how to extract in R years and month only... I researched the web a lot... and then i found your blog. Vielen DANK aus Moldawien! :)
ReplyDelete