0

I have a panel data and my time variable is defined as "YYYYMM". I need to create a dummy variable for month, however I am not quite sure if its the best solution to define the data set as time series. I was traying with the following command:

date_january <- ifelse((DATA$date == 202001),1,0)

However it is not really optimal due I have monthly data since 2010. Can anyone asist me?

Many thanks

Miguel

  • It would be helpful if you could provide us with a reproducible [minimal working example](https://en.wikipedia.org/wiki/Minimal_working_example) that we can copy and paste to better understand the issue and test possible solutions. You can share datasets with `dput(YOUR_DATASET)` or smaller samples with `dput(head(YOUR_DATASET))`. Please also provide your desired output. (See [this answer](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610) for some great advice.) – ktiu Jun 19 '21 at 12:42
  • If your dates are formatted like this yyyymm, you can do something like `substr(as.character(data$date), 5,6)` to get the month, or maybe better, create another column to have a real date, for instance, take first day of each month and extract month from it with lubridate functions. `data$date2 <- paste0(data$date, "01");data$date2 <- lubridate::ymd(data$date2);data$month <- lubridate::month(data$date2)`. Not sure I have well understood your question but looking at lubridate package may help you in any case. – Guillaume Jun 19 '21 at 16:00

0 Answers0