mardi 19 octobre 2021

R - Label a dataframe with if or ifelse and grepl

I have a dataset that captures the data of two e-learning modules. An e-learning module is indicated with a code that consists of numbers and letters. For example 1A21 and 2N34.

With the following code I am able to create a column in which I give the module code a name. To make it more understandable to me as a human.

df$Module <- ifelse(grepl("1A21", df$ModuleCode), "R-101", "R-201")

The code works like a charm. However, I am curious, how does it work if your dataframe start capture data of more than two e-learning modules? How would one solve this?

If this question is a duplicate, I apologize in advance.

Aucun commentaire:

Enregistrer un commentaire