Skip to content

Instantly share code, notes, and snippets.

@micstr
micstr / dateloopexamples.R
Created March 19, 2015 08:44
Looping through dates in R - examples of issues with indexing with dates.
# examples of issues with indexing with dates for will turn to number
# Solution
# - DONT use dates as elements of your index
# - DO index alonmg length of your date list
library(data.table)
date.list <- c(as.Date("2015/3/1"), as.Date("2015/3/2"))
# 1 loop through dates?
for (i.date in unique(date.list)){
@micstr
micstr / isdate.R
Last active May 25, 2021 11:40
Testing dates in R - an IsDate() function
# Trying to find a basic test for dates in R. was looking for a is.Date like as.Date vibe
IsDate <- function(mydate, date.format = "%d/%m/%y") {
# Check if field is a date using as.Date that looks for unambiguous dates
# Assumes date format so NA returned not Character error.
# Why? with no date format, R tries two defaults then gives error.
# BUT With a dateformat R returns NA
# Args
# Suspected date and optional date format string
# Returns