R has a built-in dataset called attitude.
Create a new discrete variable complain.level with three levels, bad
, okay
and good
.
ATTEMPT 1:
complain.level=revalue(attitude$complaints,c("bad"=(0:60),"okay"=(61:80), "good"=(81:100)))
R has a built-in dataset called attitude.
Create a new discrete variable complain.level with three levels, bad
, okay
and good
.
ATTEMPT 1:
complain.level=revalue(attitude$complaints,c("bad"=(0:60),"okay"=(61:80), "good"=(81:100)))
You can use the following:
library(dplyr)
attitude%>%mutate(complaints.level = if_else(complaints<61,"bad",
if_else(complaints>80,"good",
"okay")))
As suggested by Simon, we can also use case_when
attitude%>%mutate(complaints.level = case_when(complaints<61 ~ "bad",
complaints>80 ~ "good",
TRUE~"okay"))