I have two data frames, one with 1 column (X), and the other with 2 columns (Y & Z):
Column X contains numbers 1:99, but occasionally has some letters instead of numbers, ie: 1, 2, 3, A, 5, B, 7, 8, C, D, 11, 12 etc.
Column Y contains these same letters, which are paired (as appearing in column Z) to certain numbers, ie:
A 4
B 6
C 9
D 10
How can I replace the letters in column X with the values of column Z, according to whether the letters in column X match with the letters in column Y? This would result in column X being 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 etc.
A straightforward merge won’t work (I need to retain all values in X) and I’m not sure how I can use sub conditionally. Also, column Y and Z contain more rows than needed for column X, so I can’t just use cbind. I’m not very skilled at using regex, although that is probably my best bet…
Any help would be greatly appreciated!
I’d just use a for loop. Creating your data:
We need to make sure things are character vectors, not factors, for testing equality
Then we can do the replacing:
Finally, I’m guessing you want the
Xas numeric now that all the letters are gone: