Skip to content

Instantly share code, notes, and snippets.

View mattparker-wf's full-sized avatar

Matt Parker mattparker-wf

  • Workiva
  • Boulder, CO
View GitHub Profile
@mattparker-wf
mattparker-wf / get_monday.r
Created February 20, 2015 00:18
Get the Monday or Sunday preceding a given date
# Source: the masterful Marc Schwartz, in this R-help thread:
# http://r.789695.n4.nabble.com/previous-monday-date-td3786395.html
# Getting the Monday preceding any date
# See ?cut.Date for additional info
as.Date(cut(Sys.Date(), breaks = "weeks"))
# Alternatively, the preceding Sunday:
as.Date(cut(Sys.Date(), breaks = "weeks", start.on.monday = FALSE)
# An alternative approach
# First - a quick function for counting unique values that excludes NAs
count.unique <- function(x) { length(unique(x[!is.na(x)])) }
# Compare:
length(unique(NA))
count.unique(NA)