- 3 Minutes Wednesdays
- Posts
- 3MW (Text manipulation with R)
3MW (Text manipulation with R)
Guten Tag!
Many greetings from Ulm, Germany.
Text manipulation is an essential data cleaning skill. Usually, it precedes all kinds of data visualization. For example, I’ve recently created this table with {gt}
. Today I’m showing you how to do the necessary text manipulation that preceded that {gt}
work.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/401e0591-a1cc-4ef0-834e-02d703cf2252/tbl.png)
And remember: I show code images here but for your convenience the code can be found on GitHub.
Renaming long names
First, we need to load the underlying data from TidyTuesday. If we do that, we will notice that IBM has a suuuuper long name.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/ddd1088d-b0b8-4ef3-9cd4-6db84e690823/carbon_138_.png)
We can rename that long name in the company
column with mutate()
and if_else()
.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/f1911576-ae1d-410c-8510-ea8b98a9e0dd/carbon_139_.png)
Remove extra words
Now, let's get rid of words like "Platforms" or "Corporation". Similarly, we can get rid of commas. We can do all that that with str_remove()
.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/c1fd45e7-6077-44bd-9ab8-c3891b621bdf/carbon_140_.png)
Notice how this did not remove everything we wanted? That's because we need to use str_remove_all()
if want to get rid of all extra words (and not just the first that is found).
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/6816be65-1b93-4d25-9ab3-43b96cdc0c7d/carbon_141_.png)
Remove words with special characters
Removing “Inc.” and “.com” is a bit more tricky. That’s because they contain the special character .
.
You see, there’s a “language” called regular expressions (regex for short) that is used for describing patterns in texts. And functions like str_remove_all()
use this regex language. So far, we have used very basic regex and instructed R to remove all patterns that match e.g. “Corporation”.
If we wanted to instruct R to remove all patterns that match “Corporation” followed by any character (or white space) we’d use str_remove_all(company, 'Corporation.')
. That’s the purpose of .
in the regex language. It matches anything.
So, to remove a literal .
we need to “escape” it with a \
, i.e. we should write \.
in regex. But here’s the problem. In R (and not regex), \
is used to “escape” special characters too. And R will complain if you write \.
because R does not know any special character called .
.
Hence, you need to write \\.
This “escapes” the \
in R so that R “sees” \.
and does not interpret this as a special character. Instead, R will now “send” \.
to regex like we want.
For a deeper intro of regex, check out the excellent R4DS chapter. Anyway, armed with our new-found knowledge we can now remove “Inc.” and “.com”.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/2d64965c-3e74-4970-af20-b1fcd6d2bd1e/carbon_142_.png)
Remove white space
The previous output has white space around the company names. You can see that from the ”
in the output. We can remove that with str_trim()
.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/447ffa8e-71a5-454b-a3a4-f848bcf45f3a/carbon_143_.png)
Democracy visualization
Another visualization I’ve created recently is the following remake from an article of The Economist. Their interactive visualization is beautiful (you should check it out) and shows their democracy index for many countries over time.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/8c1ff90d-2d6a-4aa0-b4bb-12d3c3ca6302/image.png)
Here, I had to do data cleaning as well. First, I had to manually copy last year’s democracy scores from a PDF into a text file. Then, I could read the data.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/c93ce4c9-1892-4369-9687-ed0161e728a4/carbon_144_.png)
Read first number
With parse_number()
we can extract the first number from that. Conveniently, this first number is our desired democracy index.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/af5624ca-b88a-4acc-8972-fe422647c6a2/carbon_145_.png)
Fix parsing errors
It seems that there are parsing errors for two countries that both suspiciously have a -
in their name. Let’s replace those with str_replace()
before using parse_number()
.
![](https://media.beehiiv.com/cdn-cgi/image/fit=scale-down,format=auto,onerror=redirect,quality=80/uploads/asset/file/bffe8370-ac2a-4f80-9caf-1ba95bec5eee/carbon_146_.png)
This concludes this week’s 3MW. In the subscribers-only part, we continue cleaning this data set.
![](https://media.beehiiv.com/cdn-cgi/image/format=auto,onerror=redirect/uploads/publication/logo/dcd4eaf2-0a16-4f6e-a277-324841b39e98/thumb_Twitter_header_-_6.png)
Subscribe to 5 Minutes Wednesdays to read the rest.
Become a paying subscriber of 5 Minutes Wednesdays to get access to this post and other subscriber-only content.
Already a paying subscriber? Sign In
Reply