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:
- 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.
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:
- 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.
// 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
// Return the 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.
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.
How to Split Text Strings in Displayr using R
Article is closed for comments.