R - assigning test (in ifelse) to be between two numbers for generating a dummy variable

2090 views r
1

Current code:

data$light <- ifelse(data$cigs == 1:5, 1, 0)

With the code above, I'm trying to focus on getting ifelse to only assign a 1 to values 1 through 5 in cigs (cigs being number of cigarettes smoked per day, ranging from 0 to 50).

answered question

1 Answer

0

Try %in%

data$light <- ifelse(data$cigs %in% c(1:5), 1, 0)

Or equivalently

data$light <- ifelse(data$cigs >=1& data$cigs <=5, 1, 0)

posted this

Have an answer?

JD

Please login first before posting an answer.