[R] Sequential date by group

Miluji Sb m||uj|@b @end|ng |rom gm@||@com
Mon Sep 2 19:34:12 CEST 2019

Dear all,

I have a panel data with a large number of groups and the cumulative number
of months (1 - 372) for January 1995 to December 2005. My goal is to
extract the corresponding month and year for each observation.

I tried the following;

x %>%
  group_by(id) %>%
  do( data.frame(., Date= seq(.$startdate,
                              as.Date('1975-01-01'), by = '1 month')))

However, I get the following error;

Error in seq.default(.$startdate, as.Date("1975-01-01"), by = "1 month") :
  'from' must be of length 1

Essentially, I want to convert the month number (1-372) to January 1975 to
December 2005 by ID. Is that possible? Any help will be highly appreciated.

### Data ###
x <- structure(list(id = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L), month =
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), startdate = structure(c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "1975-01-01", class = "factor"),
    enddate = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label =
"2005-12-31", class = "factor")), class = "data.frame", row.names = c(NA,

Cross-posted in Statalist - however, Stata seems to have a very inflexible
date-time structure.

Best regards,


	[[alternative HTML version deleted]]

More information about the R-help mailing list