How to create a dummy variable in R using ifelse() command

时光怂恿深爱的人放手 提交于 2020-12-26 08:30:40

问题


I am trying to create a dummy variable for R. The thing is there are many categorical variables under my dataset of restaurants 'type'. Among them, I want Vegan restaurants to have value 1 and the rest to be 0. So when I run summary of the regression, I get the intercept, and b1 as reviews_number and b2 as vegan restaurants. For example, a non-vegan restaurant would be y=b0+b1(reviews_number) and a vegan restaurant will be y=b0+b1(reviews_number)+b2(Vegan). The hint is to use ifelse()command, but I can't seem to simplify the coefficients to just 3. Or else, I need to create a value for each type of restaurant respectively......


回答1:


Assuming your data frame is called df, you can create your dummy variable (Vegan) using:

df$Vegan <- ifelse(df$type == "Vegan", 1, 0) # where variable type is type of restaurants 

However, you should note that if type is a stored as factor, you can also get the coefficient on each type of restaurants (compared to the reference level) using y=b0+b1(reviews_number)+b2(type) i.e. y~reviews+type, as pointed by @mlt.




回答2:


If you need just one dummy variable, distinguishing vegan vs. non-vegan, then you can just do:

df$Vegan = as.integer(d$type == "Vegan")


来源:https://stackoverflow.com/questions/52461445/how-to-create-a-dummy-variable-in-r-using-ifelse-command

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!