Sometimes, your survey data may contain information about the date the respondents completed the survey stored as ambiguous categories rather than date values. Variables like this could be things like Week for the week of the survey, Wave for wave of the survey, or Year for year of the data. These will all be categorical variables with a Nominal Structure in Displayr, but you may want to convert them to a Date/Time variable to ensure the ordering is sequential and allow for previous period statistical testing.
There are various ways to do this outlined in How to Create Date Variables in Displayr. This article shows you a specific method of encoding the date using a new JavaScript variable. You can use this to go from a variable with weekly categories, going from this:
To a variable where the Structure is Date/Time and can be aggregated to different periods and is based on a timeline.
Requirements
- A data set loaded into Displayr containing a categorical variable that requires converting to a Date/Time variable, e.g., wave, year, quarter, month, or week.
Method
- Hover over any variable in your Data Source tree > Plus (+) > Insert Variable(s) > Custom Code > JavaScript - Numeric.
- For the example above, copy and paste this code into the Code Editor pane.
if (week == 1) Q.EncodeDate(2021,01,04);
else if (week == 2) Q.EncodeDate(2021,01,11);
else if (week == 3) Q.EncodeDate(2021,01,18);
else if (week == 4) Q.EncodeDate(2021,01,25);
else if (week == 5) Q.EncodeDate(2021,02,01);
else if (week == 6) Q.EncodeDate(2021,02,08);
else if (week == 7) Q.EncodeDate(2021,02,15);
else if (week == 8) Q.EncodeDate(2021,02,22);
else if (week == 9) Q.EncodeDate(2021,03,01);
else if (week == 10) Q.EncodeDate(2021,03,08);
else if (week == 11) Q.EncodeDate(2021,03,15);
else if (week == 12) Q.EncodeDate(2021,03,22);
else if (week == 13) Q.EncodeDate(2021,03,29);
else if (week == 14) Q.EncodeDate(2021,04,05);
else if (week == 15) Q.EncodeDate(2021,04,12);
else if (week == 16) Q.EncodeDate(2021,04,19);
else if (week == 17) Q.EncodeDate(2021,04,26);
else if (week == 18) Q.EncodeDate(2021,05,03);
else if (week == 19) Q.EncodeDate(2021,05,10);
else NaN; - Give the variable a Label and Name under the General tab in the object inspector.
- Finally, change the variable Structure to Date/Time to use it as a date variable set.
- Click Calculate.
In the JavaScript code above, I've used the input variable week, the categorical variable in my data set that I needed to convert. You will need to replace "week" in the code above with the variable's name that contains your categorical date variable. You will also notice that I've set my week start dates within parentheses in the Q.EncodeDate() function. You will need to update these specific dates that are appropriate for your data. If you are using a categorical variable that represents "month" for example, you would use:
if (month == 1) Q.EncodeDate(2021,01,01);
else if (month == 2) Q.EncodeDate(2021,02,01);
and so on and so forth.
You can further adjust the date aggregation on your new Date/Time variable by clicking on Data Values > Date/Time from the object inspector and set the aggregation level to your liking.
Technical Notes
If you have a Wave variable where you still want to show Wave 1, Wave 2, etc in the headers instead of a date, you will want to follow the instructions in How to Test Against the Previous Period without a Date/Time Variable.
Next
How to Work with Aggregated Dates Using R
How to Conduct Significance Tests by Comparing to Previous Time Periods
How to Test Against the Previous Period without a Date/Time Variable