Reputation: 370
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
Upvotes: 0
Views: 191
Reputation: 269481
1) Assuming the inputs in the Note at the end sapply over the target values.
cbind(dat, +sapply(target, `==`, substr(dat$ZZZ, 1, 3)))
giving:
ZZZ M41 F12
1 M41 1 0
2 M410 1 0
3 M42 0 0
4 F128 0 1
2) If every element of ZZZ is to be used in a target then use
target <- unique(substr(dat$ZZZ, 1, 3))
instead of the value for target
shown in the Note.
3) If every element of ZZZ is to be used in a target then another approach is:
res <- cbind(dat, model.matrix(~ substr(dat$ZZZ, 1, 3) + 0, dat))
names(res) <- sub(".*\\)", "", names(res))
res
giving:
ZZZ F12 M41 M42
1 M41 0 1 0
2 M410 0 1 0
3 M42 0 0 1
4 F128 1 0 0
target <- c("M41", "F12")
dat <- structure(list(ZZZ = c("M41", "M410", "M42", "F128")),
class = "data.frame", row.names = c(NA, -4L))
Upvotes: 1