I have a question similar to one that has already been asked: Given start date and end date, reshape/expand data for each day between (each day on a row)
This is a subset of my data (and not all variables are included; there are 43 variables in total):
start_date <- as.Date(c("1946-01-01", "1966-01-01","1979-03-01", "1966-01-01", "1988-05-01"))
end_date <- as.Date(c("1946-03-01","1966-03-01","1979-05-01", "1966-03-01", "1988-07-01"))
dyad_id <- c(260,260,260,306,306)
armsproc <- c("moderate", "low", "low", "low", "low")
gov_support <- c("explicit", "no", "no", "no", "explicit")
terrcont <- c("yes", "no", "no", "yes", "yes")
x <- data.frame(start_date, end_date, dyad_id, armsproc, gov_support, terrcont)
This is the visualisation of my sample data:
start_date end_date dyad_id armsproc gov_support terrcont
1 1946-01-01 1946-03-01 260 moderate explicit yes
2 1966-01-01 1966-03-01 260 low no no
3 1979-03-01 1979-05-01 260 low no no
4 1966-01-01 1966-03-01 306 low no yes
5 1988-05-01 1988-07-01 306 low explicit yes
Instead of a data range, I would like to have monthly data for each month between start_date and end_date. Furthermore, and what isn't answered in the question linked above, I want the data from all remaining columns simply duplicated for all the months in the time period. To be clear, I would like this data replication to be done within each dyad_id. I want something that looks like this:
month dyad_id armsproc gov_support terrcont
1946-01-01 260 moderate explicit yes
1946-02-01 260 moderate explicit yes
1946-03-01 260 moderate explicit yes
1966-01-01 260 low no no
1966-02-01 260 low no no
1966-03-01 260 low no no
1979-03-01 260 low no no
1979-04-01 260 low no no
1979-05-01 260 low no no
1966-01-01 306 low no yes
1966-02-01 306 low no yes
1966-03-01 306 low no yes
1988-05-01 306 low explicit yes
1988-06-01 306 low explicit yes
1988-07-01 306 low explicit yes
I tried using code similar to that suggested in the other question
x %>%
rowwise() %>%
do(data.frame(dyad_id=.$dyad_id, month=seq(.$start_date,.$end_date,by="1 month")))
but this simply produced the following data frame with only 2 columns:
# A tibble: 6 x 2
dyadid month
<int> <date>
1 462 1946-06-01
2 462 1946-07-01
3 463 1952-04-01
4 464 1967-03-01
5 464 1967-04-01
6 464 1967-05-01
I would be very grateful if somebody could help me out here! Cheers