mercredi 23 septembre 2020

Replace multiple occurences of a string pattern in data table

I have a data table where one of the column contains characters (ID). Some of its rows have a certain pattern (AB_). I would like to replace them with NA using a data.table solution. Input:

dt <- data.table(
ID = c("AB_1","AB_2","b","AB_3","a","c"),
col2 = 1:6,
col3 = 7:12)

Output:

dt <- data.table(
ID = c("NA","NA","b","NA","a","c"),
col2 = 1:6,
col3 = 7:12)

thank you

Aucun commentaire:

Enregistrer un commentaire