Creating a datetime variable from a categorical wave variable using R
A datetime variable in Crunch is a nice thing to have - it allows you to make line charts, to roll-up time into different bands (yearly, monthly, daily, etc) and do time-series analysis (eg: moving averages and smoothers).
However, if you have time information stored as a categorical variable (eg: Wave 1, Wave 2, Wave 3, etc. or perhaps Quarter 1, Quarter 2, etc) then you won't be able to do the things mentioned above, because you require a timestamp.
Sometimes you can get a timestamp (ie: a datetime variable) directly from a data collection platform. But sometimes you can't get this, and the only thing you have to work with is a categorical variable. In that case, you may like to encode a datetime variable from the categorical variable. You can do this in R.
Note: this method is only considered to be a stop-gap. The web app will eventually handle this conversion.
It is actually quite simple to do with the following steps.
- Set the labels of the categories to be in the %Y-%m-%d format.
- You can do this either in the web app or in R.
- For example, as 2020-01-01 or 2020-02-01 or whatever day you want to set for each wave period.
- Login and load your dataset in R
- Use the code below to derive a new variable, where:
- ds$wave - is the name of the categorical variable mentioned at step 1 above
- ds$var_date - is the alias of the variable you'll store it in.
ds$var_date <- as.Datetime(as.character(ds$wave), "%Y-%m-%d")