r - Counting dates that don't exist -
i working on data frame contains 2 columns follows:
time frequency 2014-01-06 13 2014-01-07 30 2014-01-09 56
my issue interested in counting days of frequency 0. data pulled using rpostgresql/rsqlite there no datetime given unless there value (i.e. unless frequency @ least 1). if interested in counting these dates don't exist in data frame, there easy way go doing it? i.e. if consider date range 2014-01-01 20-14-01-10, want count 7
my thought brute force create separate dataframe every date (note 4+ years of dates immense undertaking) , merging 2 dataframes , counting number of na values. i'm sure there more elegant solution i've thought of.
thanks!
sort date , gaps.
start <- as.date("2014-01-01") time <- as.date(c("2014-01-06", "2014-01-07","2014-01-09")) end <- as.date("2014-01-10") time <- sort(unique(time)) # include start , end dates, missing dates 1/1-1/5, 1/8, 1/10 d <- c(time[1]- start, diff(time) - 1, end - time[length(time)] ) d # [1] 5 0 1 1 sum(d) # 7 missing days
and days missing...
(gaps <- data.frame(gap_starts = c(start,time+1)[d>0], gap_length = d[d>0])) # gap_starts gap_length # 1 2014-01-01 5 # 2 2014-01-08 1 # 3 2014-01-10 1 (g in 1:nrow(gaps)){ start=gaps$gap_starts[g] length=gaps$gap_length[g] for(i in start:(start+length-1)){ print(as.date(i, origin="1970-01-01")) } } # [1] "2014-01-01" # [1] "2014-01-02" # [1] "2014-01-03" # [1] "2014-01-04" # [1] "2014-01-05" # [1] "2014-01-08" # [1] "2014-01-10"
Comments
Post a Comment