Created
June 4, 2012 19:35
-
-
Save iros/2870367 to your computer and use it in GitHub Desktop.
Padding a Time Series in R
This file contains 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
# Read a comma-delimited file that has the following content | |
# time,observations | |
# 2011/11/01,12 | |
# 2012/01/01,320 | |
# 2011/12/01,100 | |
# 2012/06/01,7 | |
raw.data <- read.delim("timefill.csv", header=T, sep=",") | |
# Convert the time column to a date column. | |
# Accessing a column is done by using the '$' sign | |
# like so: raw.data$time. | |
raw.data$time <- as.Date(raw.data$time) | |
# sort the data by time. The [*,] selects all rows that | |
# match the specified condition - in this case an order function | |
# applied to the time column. | |
sorted.data <- raw.data[order(raw.data$time),] | |
# Find the length of the dataset | |
data.length <- length(sorted.data$time) | |
# Find min and max. Because the data is sorted, this will be | |
# the first and last element. | |
time.min <- sorted.data$time[1] | |
time.max <- sorted.data$time[data.length] | |
# generate a time sequence with 1 month intervals to fill in | |
# missing dates | |
all.dates <- seq(time.min, time.max, by="month") | |
# Convert all dates to a data frame. Note that we're putting | |
# the new dates into a column called "time" just like the | |
# original column. This will allow us to merge the data. | |
all.dates.frame <- data.frame(list(time=all.dates)) | |
# Merge the two datasets: the full dates and original data | |
merged.data <- merge(all.dates.frame, sorted.data, all=T) | |
# The above merge set the new observations to NA. | |
# To replace those with a 0, we must first find all the rows | |
# and then assign 0 to them. | |
merged.data$observations[which(is.na(merged.data$observations))] <- 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've updated your gist a bit. Sorry, was looking for an old bocoup blog post and my brain saw "R" and it was off to the races.