I am looking for a sort a macro that i could do under R, to create a variable from another one. I want to know if in the ZZZ variable, the string starts with a certain 3 first characters (by using the substr function). And so i would name the created variable by the name of the string that i am looking for. Some example to be more explicit :
My data is like:
| ZZZ |
|---|
| M41 |
| M410 |
| M42 |
| F128 |
I would like to create a loop like this:
data$xxx = ifelse(substr(data$ZZZ,1,3)=="xxx",1 ,0)
And so the data after would look like :
| ZZZ | M41 | F12 |
|---|---|---|
| M41 | 1 | 0 |
| M410 | 1 | 0 |
| M42 | 0 | 0 |
| F128 | 0 | 1 |
Because i must do it for more than a hundred times, so creating a loop would help to decrease the size of the program.
I don't know if we can be able to do this type of macros under R.
Thanks for reading, BP
Aucun commentaire:
Enregistrer un commentaire