When I’m working in R with text data parsed from online
opinion forums and social webs (e.g. twitter), I need to do some cleaning and
pre-processing such as removing punctuation marks, striping extra white spaces, or
converting text to lower case.
More often than not, when using the tolower()
function I encounter myself with a really annoying error that is a truly pain in the butt.
Consider the following example. Let’s say we have the text from a tweet in an object
called some_text. When we print the object in the console, we get a warning message
(in red) like this:
So, how can we solve this error? Meet the tryCatch
function! This function will help us to catch possible errors. We’ll make a new function
combining tryCatch() and tolower() so we can identify any undesirable text
without returning any ugly message and without stopping our programs.
Here’s my tryTolower() function
Let’s test it
Suppose you have a character vector with five elements