-
-
Save ryanpeek/8bd8b386d38f14089a15 to your computer and use it in GitHub Desktop.
All the ways I know to get things in the right date/time format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# All of my times are in mm/dd/yyyy hh:mm:ss | |
# From a character string (read in from read.csv(stringsAsFactors = FALSE)) | |
# By far the easiest way is with lubridate::mdy_hms() | |
#Ex: | |
options(stringsAsFactors = FALSE) | |
Sys.timezone(location = FALSE) | |
Sys.setenv(TZ = "Pacific/Pitcairn") | |
Sys.timezone() # make sure it took | |
files <- list.files(pattern = "*.csv") #create list of files by character name; change working directory first | |
d <- plyr::adply(files, 1, read.csv) #combine into one big dataframe | |
colnames(d) <- c("DateTimeUTC", "Monitor" , "TagID") | |
d$DateTimeUTC <-mdy_hms(f$DateTimeUTC) # Convert to POSIXct | |
# Another example of converting from yearday (or Julian day) back to a full POSIXct date | |
#---# Assuming you have a year, month and yday column, you can back calculate a POSIXct date/datetime column | |
# OPTION 1: If you have separate year ("yr"), month ("mon"), year day ("yday") columns: | |
date<-ymd(strptime(paste0(yr,"-", mon,"-", yday),format = "%Y-%m-%j") | |
# OPTION 2: If you only have year ("yr") and year ("yday") day columns | |
date<-as.Date(x= (yday - 1), origin=paste0(yr, "-01-01")) | |
# this is with times | |
datetime<-ymd_hms(strptime(paste0(yr,"-", mon,"-", yday, " ", hour,":00"),format = "%Y-%m-%j %H:%M") | |
# Add a water year column (water year starts on Oct 01 and ends Sep 30) | |
# Make Water Year column from date column (assumes your column is already POSIXct or as.Date) | |
wtr_yr <- function(dates, start_month=10) { | |
# Convert dates into POSIXlt | |
dates.posix = as.POSIXlt(dates) | |
# Year offset | |
offset = ifelse(dates.posix$mon >= start_month - 1, 1, 0) | |
# Water year | |
adj.year = dates.posix$year + 1900 + offset | |
# Return the water year | |
adj.year | |
} | |
## AN EXAMPLE | |
library(lubridate) | |
library(dplyr) | |
sometime<-seq(ymd('2011-10-01'),ymd('2015-09-30'), by = '1 day') # create a seq of dates | |
jday<-yday(sometime) # add julian day | |
WY<-wtr_yr(sometime) # use water year function to add water year (Oct 1 through Sep 30) | |
df<-data.frame("sometime"=sometime, "jday"=jday, "WY"=WY) # bind dataframe | |
# this method takes leap years into account | |
df$wyd<-ave(df$WY, df$WY, FUN=seq_along) # break by water year, df must be ordered by date | |
# use dplyr to double check data... | |
df %>% group_by(WY) %>% summarize("days"=n()) | |
# Rough way to do this, need a POSIXct formatted date column "date" | |
wyday<-ifelse(month(df$date)<10, (yday(df$date)+92), | |
(yday(df$date)-273)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment