jeudi 16 novembre 2017

How to test if the first three characters in a string are letters or numbers in r?

The example of the dataset I have is given below, note that I have more than two columns in the total dataset.

ID   X
1   MJF34
2   GA249D
3   DEW235R
4   4SDFR3
5   DAS3

I want to test whether the first three characters in X are letters, if they are then I want to replace that value to show only the first three letters. If the first three characters aren't letters then I want to replace those values with "FR". Hence the result would be as follows.

ID    X
1    MJF
2    FR
3    DEW
4    FR
5    DAS

Currently X is a character data type.

Thanks in advance for any help.

Aucun commentaire:

Enregistrer un commentaire