Frodo Baggins
Frodo Baggins

Reputation: 45

Dummy variable with multiple conditions

Suppose I have a dataframe "df" with 2 columns: x and y. They contain numerical real numbers (both positive and negative). I need to create a dummy variable named, dummy_var, which follows the following condition:

dummy_var = 1 if , -100 < x < -90, -80 < x < -50, and 50 < y < 45

dummy_var = 0, otherwise

This is what I tried:

df$dummy_var = 0
df$dummy_var[df$y > 50, df$y < 45 , df$x > -100,df$x < -90, df$x > -80 
& df$x < -50 ] = 1

I am getting error after placing the minus sign after the relational operator. Not sure how to debug it.

incorrect number of subscripts on matrix
Traceback:

Also, the code that I wrote is probably inefficient. So, if you have better suggestions to write the code, that would also be very helpful.

Upvotes: 1

Views: 1029

Answers (1)

akrun
akrun

Reputation: 887108

We can use | with & to create the logical expression

i1 <- with(df, (x > -100  & x <- 90)|(x > -80 & x < -50)|(y > 50 & y < 45))
df1dummy_var[i1] <- 1

Upvotes: 2

Related Questions