This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
economics:r:text-manipulation [2018/10/23 14:03]
Olivier Simard-Casanova created
— (current)
Line 1: Line 1:
-# Manipulate text variables 
-Many real life databases were not created for scientific or analytic purposes. In other words, they could be dirty/​messy. 
-This page is especially useful if you need to extract or work with string/text variables. 
-## Create a dummy variable based on text 
-Assume you have a string variable, and depending on the presence (or not) of some text, you want to create a new binary variable taking the value 0 or 1. 
-df$dummy <- as.numeric(2) 
-df$dummy[grepl("​a specific string",​ df$varToProcess,​ fixed = TRUE)] <- as.numeric(0) 
-df$dummy[grepl("​another specific string",​ df$varToProcess,​ fixed = TRUE)] <- as.numeric(1) 
  • Last modified: 16 months ago
  • by Olivier Simard-Casanova