Skip to content

Instantly share code, notes, and snippets.

@nickp60
Created July 31, 2018 14:48
Show Gist options
  • Select an option

  • Save nickp60/2381790f9d6cfbc3ad0a842a94f83aa0 to your computer and use it in GitHub Desktop.

Select an option

Save nickp60/2381790f9d6cfbc3ad0a842a94f83aa0 to your computer and use it in GitHub Desktop.
Vectorized date selection in R
# I made some slighlty bigger test data to make sure we had some we weren't interested in
# here are all the dates in the two years between dec 2011 and 2013
all_dates <- seq(as.Date("2011-12-15"), as.Date("2013-12-15"), "day")
# like you did, randomly fill in value, and randomly sample for the available nodes
bigtest <- data.frame(
time = all_dates,
node = sample(c(206,211,301,108), size = length(all_dates), replace=T),
value = rnorm(length(all_dates), 21, 2)
)
# make sure these are read as dates, otherwise the equality testing might not work
# (I think they would get converted to date types, but better not to assume)
sampling_date = as.Date(c("2012-12-18", "2013-03-13", "2013-07-12"))
# make a column to mark whether we include the date or not
bigtest$keep <- FALSE
# there is no way to vectorize this that I can think of, so we have to loop
# through each sampling date. Not too timeconsuming though, as the inner bit is still vectorized
for (d in sampling_date){
bigtest$keep <- ifelse(
# if the date is equal to or before the samplingdate AND the date is after (samplingdate - 7 days)
bigtest$time <= d & bigtest$time > d - 7,
# mark that row
TRUE,
# otherwise, keepo the previous value (this keeps us from overwriting previous TRUEs)
bigtest$keep)
}
# there should be 21 -- 7 for each week preceding each of the 3 sampling day
table(bigtest$keep)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment