0

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

Lee Tagziria
  • 53
  • 2
  • 10

1 Answers1

1

I apologise wholeheartedly, but I have found a previous question that answers my question! I had searched on stackoverflow for a good hour before posting my question and couldn't find what I was looking for. This link to the related question is: R -- Expand date range into panel data by group

For anyone that may be interested, I used the following code (note: this code is for my complex dataset, not the sample dataset I used in my question):

f <- function(x) with(x, data.frame(dyadid, extraterritorial, rebpolwing,
                                    rebpolwinglegal, rebestimate, rebstrength,
                                    centcontrol, strengthcent, mobcap, armsproc,
                                    fightcap, terrcont, terrname, effterrcont,
                                    conflicttype, transconstsupp, rebextpart,
                                    rebpresosts, presname, rebel.support,
                                    rtypesup, rsupname, gov.support, gtypesup,
                                    gsupname, govextpart,
                                    date = seq(start_year_month, end_year_month, by = "month")))

NSA2 <- do.call("rbind", by(NSA1, 1:nrow(NSA1), f))
Lee Tagziria
  • 53
  • 2
  • 10