Ben
Ben

Reputation: 25

How do I create a dummy variable within a range of values in R?

I need to create a dummy variable where if 20≤dummy≤40, it reads '1', and '0' otherwise. I think I am having a brainfart, but I cannot seem to do it.

I currently have:

dummy_x <- ifelse(x<=40, 1, 0)

which works, however:

dummy_x <-ifelse(20<=x<=40, 1, 0)

does not. If anyone can help, it would be greatly appreciated.

Upvotes: 1

Views: 1125

Answers (1)

Ruam Pimentel
Ruam Pimentel

Reputation: 1329

You have to split the second ifelse into two conditions like ifelse(x>=20 & x <=40, 1, 0)

Upvotes: 1

Related Questions