Jose Morales
Jose Morales

Reputation: 9

Creating a Dummy Variable

I am working on birthweight data with categories cigs smoked per day, male, white.

I am trying to create dummy variable for nonsmoker which I used nonsmoker<-ifelse(data$cig==0,1,0), but for light smokers, which would be between 1-5 cigarettes per day, I'm not sure on how to set a series.

I thought maybe light smoker<-ifelse(data$cigs==1-5,1,0) would work, but guess not.

Upvotes: 1

Views: 1107

Answers (1)

Simon
Simon

Reputation: 10150

You would need to use a boolean to select values greater than 1 and less than 5. So something like:

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

You could also use the cut method to create a factor, the levels of which you could then rename:

cig <- 0:10  # your original data
smoker <- cut(cig , c(-1, 0, 5, 10))
levels(smoker) <- c("0", "1-5", "6-10")  # rename the levels

which will then give you a factor that looks like this:

> smoker
 [1] 0    1-5  1-5  1-5  1-5  1-5  6-10 6-10 6-10 6-10 6-10

You can set your cut boundaries (i.e. is the range inclusive/exclusive of the end points?) by playing with the include.lowest and right arguments of the cut method

Upvotes: 1

Related Questions