String splitting is the process of breaking up a text string in a systematic way so that the individual parts of the text can be processed. This article describes how to go from a single variable where commas separate the 1st, 2nd, and 3rd mentions, and so on ...
To storing each mention in a separate variable:
Requirements
- A data set loaded into Displayr that contains a text variable where multiple responses are stored in a single variable as comma-separated text values.
Method
In Displayr, to split the text variable requires adding new variables that will appear alongside the original text variable in the Data Set tree, similar to below:
To do so, follow these steps:
- Either hover over any variable in your Data Sets tree > Plus (+) > Insert Variable(s) > Custom Code > JavaScript - Text or else from the toolbar menu, select Anything > Data > Variables > New > Custom Code > JavaScript - Text. A new variable will be created in the Data Sets tree.
- With this new variable selected, go to the object inspector on the right of your screen and under Properties > GENERAL give the variable a Label, a Name and the Structure by default will be set to Text.
- Next, copy and paste the code below into the JAVASCRIPT CODE box:
// Define the text variable to split
var string = awareness
// Define the delimiter
var split_string = string.split(",");
// Extract the first string of before the delimiter (first string field = 0, second string field = 1 etc)
var result = split_string[0]
// Return the result
result - Click Calculate.
This code does the following things:
The split() method is used to split a string into an array of substrings and it returns a new array.
Tip: If an empty string ("") is used as the separator, the string is split between each character.
Note: The split() method does not change the original string.
The syntax:
string.split(separator, limit)
OPTIONAL: separator specifies the character, or the regular expression, to use for splitting the string, as in the example above, ",". If omitted, the entire string will be returned (an array with only one item).
OPTIONAL: limit is an integer that specifies the number of splits, items after the split limit will not be included in the array.
Next
How to Split Text Strings in Displayr using R
How to Work with JavaScript in Displayr