Skip to content

Instantly share code, notes, and snippets.

@whatever
Created March 14, 2013 14:45
Show Gist options
  • Save whatever/5161909 to your computer and use it in GitHub Desktop.
Save whatever/5161909 to your computer and use it in GitHub Desktop.
Creates a matrix of random dates
get.random.date <- function () {
# Ignore this line... I was going to make it *ACTUALLY* do a random date,
# but we don't need this for testing... Just have 28 be an upper bound on the month
# and we don't have to worry about which month we're in or whether it's a leap year
months <- list(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
# This should determine if it's a leap year... which just changes
is.leap.year <- function (y) {
if (y %% 4 != 0)
return(FALSE)
if (y %% 100 != 0)
return(TRUE)
if (y %% 400 != 0)
return(FALSE)
return(TRUE)
}
day <- sample(1:28, size = 1)
month <- sample(1:12, size = 1)
year <- sample(2010:2013, size = 1)
paste(month, day, year, sep = "/")
}
get.random.time <- function () {
# Get date component
date <- get.random.date()
# Generate time component
hour <- sample(0:23, size = 1)
minute <- sample(0:59, size = 1)
second <- sample(0:59, size = 1)
# IGNORED
# meridian <- sample(c("AM", "PM"), size = 1)
time <- paste(hour, minute, second, sep = ":")
paste(date, time)
}
# Must be initialized as NULL so that rbind attached correctly
data.dates <- NULL
for (k in 1:100) {
date.Date <- as.Date(get.random.date(), "%m/%d/%Y")
time.POSIXlt <- as.POSIXlt(get.random.time(), format = "%m/%d/%Y %H:%M:%S")
time.POSIXct <- as.POSIXct(get.random.time(), format = "%m/%d/%Y %H:%M:%S")
tiny.frame <- data.frame(Date = date.Date, POSIXlt = time.POSIXlt, POSIXct = time.POSIXct)
data.dates <- rbind(data.dates, tiny.frame)
}
data.dates
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment