Method - Using JavaScript
Removing text from a Text variable
To remove text from within a Text variable:
- Create > Variables and Questions > Variable(s) > JavaScript Formula(s) > Text.
- In the Expression field, type an expression using the replace method. For example, if your variable was called Vcust1, and it contained commas that you wished to remove, you would write the expression: (Vcust1).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. See Manipulating Text with JavaScript for more ways of manipulating text.
- Press OK.
Removing text from a text variable when creating a numeric variable
When you change the Question Type from a Text question to Number, Q automatically converts the text to numbers. However, if a non-numeric character is encountered, Q 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:
- Change the Question Type to Number.
- Right-click on the new variable that is created and select Edit Variable.
- You will see an expression such as Q.AsNumeric(Vcust1);, where Vcust1 is the name of the original text variable. Modify this expression by replacing Vcust1 with another expression that replaces the text. For example, if replacing commas, as in the example above, your final expression would be Q.AsNumeric((Vcust1).replace(/,/g,""));.
- Press OK.
Method - Using R
In Q, you don't need to specify whether an R variable should be numeric or text ahead of time. Given this, there are two functions in R that you can use to remove or replace text in responses. For those with advanced coding skills, you can even use regular expressions, see How to Work with Regular Expressions Using R.
Replacing entire responses using grepl()
If you'd like to replace the entire text response from a respondent if they mention a certain word(s) then you can use the grepl() function.
For example, to recode your text variable Q1OpenEnd to be blank if "N/A" was mentioned in the response:
1. Create an R variable in your dataset, see How to Create a Custom R Variable.
2. Use the following code in the variable and modify it to your specific needs.
#set the values to be equal to the open end to start
x=Q1OpenEnd
#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 then you can use the gsub() function.
For example, to swap all mentions of "Diet Pepsi" with "Pepsi Products" in your text variable Q1OpenEnd:
1. Create an R variable in your dataset, see How to Create a Custom R Variable.
2. Use the following code in the variable and modify it to your specific needs.
#set the values to be equal to the open end to start
x=Q1OpenEnd
#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, a response of "I love Diet Pepsi" would then become "I love Pepsi Products".