I have 115 variables in my data frame, and I need to create a dummy variable for each variable that has negative value or values of 999. The dummy indicator will take a value of 1 if the original x value is <0 or 999 and 0 otherwise. For example for the following data frame
at<-c(1,-9,-1,999)
Bc<-c(1,-2,999,0)
df<-data.frame(at,Bc)
I want to have an output dataframe with the following format:
at Bc I.at I.Bc
1 1 0 0
-9 -2 1 1
-1 999 1 1
999 0 1 0
You can use a base R solution like this:
transform(df,I=ifelse(df<0|df==999,1,0))
at Bc I.at I.Bc
1 1 1 0 0
2 -9 -2 1 1
3 -1 999 1 1
4 999 0 1 0