datetime - Format date-time as seasons in R? -
It is possible to format POSIXlt date-time objects as months:
In R,
< Code> format (session time (), format = '% Y-% m')There is no way to do the same thing with the weather, or the 3 month group (DJF , MAM), JJA, son)? These divisions are really common in climatological and ecological sciences, and it would be nice to have a great way to format them quickly with months. Clearly the DJF gets more than 2 years, but for purposes or for this question, which does not really matter - only push them continuously in any given year (or, ideally, in specifying it) Will be able to enter that year).
I am using the output as an index for
by () , so the output format does not make any difference, just every year / season is unique
Edit: Example Data:
Date & lt; - Sys.Date () + seq (1,380, = 35) Date & lt; - Structure (c (16277, 16,312, 16347, 16, 382, 16417, 16,452, 16,487, 16,522, 16,557, 16,592, 16,627), 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-66 "" 2015-07-11 "
The result should be:
c (" 2014- JJ "," 2014-JJA "," 2014-SAAN "," 2015-SN "," 2015-DJF "," 2015-DJF "," 2015-DJF "," 2015-DJF "," 2015-MAM "2015-DjAJ", "2015-JJA", "2015-JJA")
But "2015-DJF" can also be "2014-DJF" In addition, the output Does not matter as - "2104q4 or 201404 will also be ok.
as.POSIXlt one named The list (which makes it inappropriate for data.frame columns) lists columns can be accessed individually and includes "year" (used for 1900-based, 1970's, in contrast to the default) and "som "(0-based). The best place to see this list in the HTC Help System is
? DateTimeClasses :
Only one weather count, then year-weather calculation
c ('DJF', 'mam', 'jja', 'son Numeric vector 1 + ((as.POSIXlt ($ Monday + 1 selection from character vector with dates)% /% 3) %% 4] [1] "JJA" "JJA" "son" " Son "DJF" "DJF" "DJF" "MAM" "MAM" "JJA" [11] "JJA" Paste (1900 + # This number is the base year for POSIXlt year as.POSIXlt (dates) $ years + 1 * (As.POSIXlt (dates) $ years == 12), #defter C (offset required for 'DJF', '0mam', JJ ',' son ') [0 to 0-based-mon 1 + (Such as POSA CXT $ MON + 1)% /% 3) %% 4], Sep = "-") [1] "2014-JJA" 2014-JJA "2014-JJ" 2014-son "2014-son" "2014-DJF" [6] "2015-DJF" "2015-DJF" "2015-MM" 2015- MAM "2015-JJA" [11] "2015-JJA"
It should not be difficult to create a function that creates the formatting you need. This is the modulo arithmetic on the posicolate values for the month and year.
Comments
Post a Comment