datetime - Format date-time as seasons in R? -


in r, it's possible format posixlt date-time objects month:

format(sys.time(), format='%y-%m') 

is there way same thing seasons, or 3-month groups (djf, mam, jja, son)? these divisions common in climatological , ecological science, , great have neat way format them months. djf falls on 2 years, purposes or question, doesn't matter - consistently shove them either year, (or, ideally, able specify year go into).

i'm using output index by(), output format doesn't matter much, long each year/season unique.

edit: example data:

dates <- sys.date()+seq(1,380, by=35) dates <- structure(c(16277, 16312, 16347, 16382, 16417, 16452, 16487,                       16522, 16557, 16592, 16627), class = "date") dates #[1] "2014-07-26" "2014-08-30" "2014-10-04" "2014-11-08" "2014-12-13" # "2015-01-17" "2015-02-21" "2015-03-28" "2015-05-02" "2015-06-06" "2015-07-11" 

should result in:

c("2014-jja", "2014-jja", "2014-son", "2014-son", "2015-djf", "2015-djf",    "2015-djf", "2015-mam", "2015-mam", "2015-jja", "2015-jja") 

but "2015-djf"s "2014-djf". also, form of output doesn't matter - "2104q4 or 201404 fine.

as.posixlt returns named list (which makes unsuitable data.frame columns). list columns can individually accessed , include "year" (1900-based, unlike 1970 used default) , "mon" (0-based). best place see list in hte system ?datetimeclasses:

first seasons calculation, year-seasons calculation

 c('djf', 'mam', 'jja', 'son')[ # select character vector numeric vector           1+((as.posixlt(dates)$mon+1) %/% 3)%%4]   [1] "jja" "jja" "son" "son" "djf" "djf" "djf" "mam" "mam" "jja" [11] "jja"       paste( 1900 + # base year posixlt year numbering               as.posixlt( dates )$year +               1*(as.posixlt( dates )$year==12) ,   # offset needed december           c('djf', 'mam', 'jja', 'son')[          # indexing 0-based-mon                              1+((as.posixlt(dates)$mon+1) %/% 3)%%4]            , sep="-")  [1] "2014-jja" "2014-jja" "2014-son" "2014-son" "2014-djf"  [6] "2015-djf" "2015-djf" "2015-mam" "2015-mam" "2015-jja" [11] "2015-jja" 

shouldn't difficult make function constructs formatting expect. modulo arithmetic on posixlt values month , year.


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -