within a data set (df) with > 600 observations and 100 variables, I have a variable with multiple characteristics in form of a character strings like in the following:
df$a
a
1 aa
2 bb
3 aa
4 cc
5 bb
6 dd
7 cc
8 dd
Now, I would like to compute a new binary variable out of a , where all "aa" and "bb" get the value 0 and all "cc" and "dd" get the value 1. I expect something like that:
a b
1 aa 0
2 bb 0
3 aa 0
4 cc 1
5 bb 0
6 dd 1
7 cc 1
8 dd 1
How would I do that?
Thank you very much in advance for any kind of help.
Magnus
with(df, ifelse(a %in% c("aa", "bb"), 0, 1))"levels<-"function;levels(DF$a) = list("0" = c("aa", "bb"), "1" = c("cc", "dd"))NewVariable <- with(df, ifelse(OldVariable %in% c("first value", "second value", "third value"), 0, 1))the new variable holds only the value 1 for all observations, also for those which should be 0. I don't know why.