This article describes how to replace or remove specific text from a text variable.
Requirements
A Text variable. Text variables are represented by a small a next to the variable in the Data Sources tree:
Please note these steps require a Displayr license.
Method - Using JavaScript
Removing text from a Text variable
To remove text from within a Text variable:
- Hover over the Data Surces tree and select + > Custom Code > JavaScript - Text;
or else from the toolbar menu, select Anything> Data > Variables > New > Custom Code > JavaScript - Text.
- Go to Data > JAVASCRIPT CODE and input an expression using the replace method. For example, if your text variable is called q1a_1 and contains commas you wish to remove, you would input the expression:
(q1a_1).replace(/,/g,"")
Here, /,/g means that you wish to find all instances of commas. If modifying this expression for other purposes, change the first comma (between the forward slashes) to whatever text you wish to find, and type whatever you wish to replace it with between the two double quotes. For more ways of manipulating text, see How to Manipulate Strings Using JavaScript. - Press Calculate.
Removing text from a text variable when creating a numeric variable
When you change the Question Type from a Text question to a Numeric, Displayr automatically converts the text to numbers. However, if a non-numeric character is encountered, Displayr will ignore it and all characters that follow. For example, 123,456,789 will change to 123. This problem is solved by replacing the text as follows:
- Select the Text variable from the Data Sources tree.
- Go to Data > Properties > GENERAL > Structure and select Numeric.
- Go to Data > JAVASCRIPT CODE, where you will see an expression such as Q.AsNumeric(q1a_1), where q1a_1 is the name of the original text variable. Modify this expression by replacing q1a_1 with another expression that replaces the text. For example, if replacing commas, as in the example above, your final expression would be:
Q.AsNumeric((q1a_1).replace(/,/g,""))
- Press Calculate.
Method - Using R
Replacing entire responses using grepl()
If you'd like to replace the entire text response from a respondent if they mention a particular word(s) then you can use the grepl() function.
For example, to recode your text variable q1a_1 to be blank if "N/A" was mentioned in the response:
- Hover over the Data Sources tree and select + > Custom Code > R - Text; or else, from the toolbar menu, select Anything
> Data > Variables > New > Custom Code > R - Text.
- Go to Data > R CODE and input the following code modified to your needs.
#set the values to be equal to the open end to start
x=q1a_1
#find values that include N/A in the response and set those to blank ""
x[grepl("N/A",x,ignore.case=T)]=""
#return the final result
x
Replacing parts of responses using gsub()
If you'd like to replace specific parts of the response, you can use the gsub() function.
For example, replace all mentions of "Diet Pepsi" with "Pepsi Products" in your text variable q1a_1:
- Hover over the Data Sources tree and select + > Custom Code > R - Text; or else, from the toolbar menu, select Anything
> Data > Variables > New > Custom Code > R - Text.
- Go to Data > R CODE and input the following code modified to your needs.
#set the values to be equal to the open end to start
x=q1a_1
#find diet pepsi in the response and replace those words with Pepsi Products
x=gsub("diet pepsi","Pepsi Products",x,ignore.case=T)
#return the final result
x
Using the code above, an "I love Diet Pepsi" response would become "I love Pepsi Products".