era 0.5.0: chronological ordering and extremes
R-bloggers 2024-11-20
[This article was first published on Joe Roe, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
era v0.5.0 is now available on CRAN:
install.packages("era")
This minor release adds functions for chronological ordering (yr_sort()
) of year vectors:
# Forward-counting era: x <- yr(c(200, 100, 300), "CE") yr_earliest(x) #> # CE years <yr[1]>: #> [1] 100 #> # Era: Common Era (CE): Gregorian years (365.2425 days), counted forwards from 0 yr_latest(x) #> # CE years <yr[1]>: #> [1] 300 #> # Era: Common Era (CE): Gregorian years (365.2425 days), counted forwards from 0 yr_range(x) #> # CE years <yr[2]>: #> [1] 100 300 #> # Era: Common Era (CE): Gregorian years (365.2425 days), counted forwards from 0 # Backward-counting era: y <- yr(c(200, 100, 300), "BCE") yr_earliest(y) #> # BCE years <yr[1]>: #> [1] 300 #> # Era: Before Common Era (BCE): Gregorian years (365.2425 days), counted backwards from 1 yr_latest(y) #> # BCE years <yr[1]>: #> [1] 100 #> # Era: Before Common Era (BCE): Gregorian years (365.2425 days), counted backwards from 1 yr_range(x) #> # CE years <yr[2]>: #> [1] 100 300 #> # Era: Common Era (CE): Gregorian years (365.2425 days), counted forwards from 0
And for calculating their extreme values (yr_earliest()
, yr_latest()
, and yr_range()
)
# Forward-counting era: x <- yr(c(200, 100, 300), "CE") yr_earliest(x) #> # CE years <yr[1]>: #> [1] 100 #> # Era: Common Era (CE): Gregorian years (365.2425 days), counted forwards from 0 yr_latest(x) #> # CE years <yr[1]>: #> [1] 300 #> # Era: Common Era (CE): Gregorian years (365.2425 days), counted forwards from 0 yr_range(x) #> # CE years <yr[2]>: #> [1] 100 300 #> # Era: Common Era (CE): Gregorian years (365.2425 days), counted forwards from 0 # Backward-counting era: y <- yr(c(200, 100, 300), "BCE") yr_earliest(y) #> # BCE years <yr[1]>: #> [1] 300 #> # Era: Before Common Era (BCE): Gregorian years (365.2425 days), counted backwards from 1 yr_latest(y) #> # BCE years <yr[1]>: #> [1] 100 #> # Era: Before Common Era (BCE): Gregorian years (365.2425 days), counted backwards from 1 yr_range(x) #> # CE years <yr[2]>: #> [1] 100 300 #> # Era: Common Era (CE): Gregorian years (365.2425 days), counted forwards from 0
Essentially these are all wrappers for base functions (sort()
, min()
, max()
and range()
) that are aware of the directionality of the era system attached to the vector: “CE” years are counted forwards, “BCE” years are counted backwards, etc.
I decided to implement them as prefixed functions instead of S3 methods for yr
vectors because I didn’t want to suprise people when they used e.g. max()
expecting the numerical maximum and got the chronologically latest value instead.
Links
To leave a comment for the author, please follow the link and comment on their blog: Joe Roe.
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.