I have start and end dates for events that I want to expand into a monthly panel, and I wanted to know if there was any tool in dplyr
for solving this problem. The following code does what I want to do with ddply()
. It first creates an example tibble
data.frame
(called "wide") where "id" represents an individual and "HomeNum" is an event for that individual. The next line creates a "date" variable that is a monthly series from "StartDate" to "FinishDate" within each "id" by "HomeNum" group.
library(plyr)
library(dplyr)
library(tibble)
wide =
tibble(
id = c(1, 1, 2, 2, 2),
HomeNum = c(0,1,0,1,2),
StartDate = as.Date(c("2001-01-01", "2001-03-01", "2000-04-01", "2001-02-01", "2002-08-01")),
FinishDate = as.Date(c("2001-02-01", "2002-05-01", "2001-01-01", "2002-07-01", "2002-12-01"))
)
panel =
ddply(wide,
~id+HomeNum,
transform,
date = seq.Date(StartDate, FinishDate, by = "month")
)
I assume that dplyr
, as the "the next iteration of plyr
", must have some way to implement a similar solution (and output a tibble
), but the following did not work:
panel =
wide %>%
group_by(id, HomeNum) %>%
mutate(date = seq.Date(StartDate, FinishDate, by = "month"))
and returned
Error in mutate_impl(.data, dots) :
Column `date` must be length 1 (the group size), not 2
Frankly, I am surprised that the ddply()
solution works and does not throw a similar error.
My implementation with ddply()
is similar to answers to this question.
You can coerce the elements of date
to lists and unnest
.
library(tidyverse)
wide %>%
group_by(id, HomeNum) %>%
mutate(date = list(seq.Date(StartDate, FinishDate, by = "month"))) %>%
unnest(date)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With