Splitting Alpha-Numeric Codes- 5 mins
This past week I was introduced to an interesting problem, splitting an alpha-numeric code into the characters and digits. For instance,
'AA11' => c('AA', '11'). Below is a sloppy but functional implementation.
Turn alpha-numeric code to a vector of two strings (the alpha and numeric).
This is the solution to the problem. However we need to generalize this solution in order to apply this to an entire data set.
Part 1 - String Splitting Logic
Let’s figure out the logic first.
- Find where the numeric portion starts. Heads up, the
'\\d'is a regular expression (regex). This is a pattern that will select the digits in a string.
- Get the alpha portion and the numeric portion
Now Encapsulate in a Function
With the logic figured out we can use it to create a function
Use Function on Vector
Now that we know that the function works let’s use it on our a vector of alphanumeric strings. This will use
lapply to loop over all the strings in the vector and return a list.
Part 2 - Convert Alpha to Numbers
An additional portion of this problem converting the characters into numbers. The original data is a grid with each grid element labeled as ‘A1, A2, …’, the goal is to instead label each row with cartesian coordinates. In this case x coordinates are the digits and y coordinates are the characters. The lowest character is 1. This bit is weird, and likely unique to this particular problem.
- First create vector of all your alpha characters. If I remember correctly
QQQ = 1. I’ve only included some of the alpha codes. These should be in order because we will use the index of each value in the vector.
- Replace the characters with numbers.
Part 3 - Replace Characters with Numbers
This is a little sloppy but it works right now. The next step is to add each element of the list into an X and Y format.
So, this is how to solve your interesting problem. It is a little sloppy but it should work.