I usually use the lubridate package for date manipulation, however, there's one item in that package that I haven't really gotten the hold of, which is making an array of dates.

I usually have a situation where I'm missing data, such as with sales data. If a sale didn't take place on a certain day, then that date value won't show up. I want that date to have a value of zero.

I can make a sequence of dates with the following (base-R) command:

date_sequence = seq(as.Date("2018/01/01"), as.Date("2018/12/31"), "days")

You can then put this in a data.frame which is easy to use with dplyr:

all_dates = data.frame(date_sequence=date_sequence)

full_dataset = all_dates %>%
    left_join(sales_data)

full_dataset[is.na(full_dataset$product_sales), 'product_sales'] <- 0
Comments

comments powered by Disqus

Published

Category

R

Tags

Contact