Skip to content

Instantly share code, notes, and snippets.

@johnDorian
Created January 19, 2015 13:46
Show Gist options
  • Save johnDorian/c98613ceedfd64894eac to your computer and use it in GitHub Desktop.
Save johnDorian/c98613ceedfd64894eac to your computer and use it in GitHub Desktop.
modify hour of a subset using lubridate
library(lubridate)
## A synthetic data set.
doc <- data.frame(date=ymd_hm("2014-01-01 14:00") + (1:360)* 24*3600)
# Find all the dates before a certain time period.
index <- doc$date < ymd("2014-06-01")
## use the hour function to extract the hour of each day e.g
hour(doc$date)
## Now using the index from above you can subtract an hour from each of the times
# using the index vector and reassiging the modified hour back to the time stamp.
hour(doc$date[index]) <- hour(doc$date[index]) - 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment