Introduction
Sometimes your survey data may contain information about the date the respondents completed the survey stored as categories rather than as date values. Suppose you have a variable in your data file called Week that encodes the week of the survey in Categorical format. This article shows you how to create a new JavaScript Variable which has date values derived from categories, going from this:
To a variable where you can change the Structure to Date/Time to use it as a date variable set.
Requirements
- A data set loaded into Displayr that contains a categorical variable that requires converting to a Date/Time variable e.g., year, quarter, month, and week.
Method
Displayr has a built-in JavaScript function Q.EncodeDate(y, m, d). It will enable you to return a number in a format that Displayr can, in turn, recognize as a date question. When used in conjunction with conditional statements, you can turn a categorical variable such as above into a date variable. Using the categories to assign date values to the respondents can allow you to make use of some of Displayr's date aggregation and time series analysis functionality. To do this:
- Either hover over any variable in your Data Sets tree > Plus (+) > Insert Variable(s) > Custom Code > JavaScript - Numeric or else from the toolbar menu, select Anything > Data > Variables > New > Custom Code > JavaScript - Numeric.
- For the example above, copy and paste this code into the JAVASCRIPT CODE in the object inspector under Properties
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 Properties > GENERAL.
- Finally, change the variable Structure to Date/Time to use it as a date variable set.
- Click Calculate.
See Also
How to Work with Aggregated Dates Using R
Comments
0 comments
Article is closed for comments.