I have a data frame in R, and I’d like to add dummy variables in order to plot different items different colors.
My data frame, df <-, is something like this:
UID CategoryA count
1 AAA 2
2 AAA 2
3 BBB 1
4 CCC 1
5 DDD 1
I have another list, special <- c("AAA", "DDD")
I’d like to end up with df something like:
UID CategoryA count dummy
1 AAA 2 1
2 AAA 2 1
3 BBB 1 0
4 CCC 1 0
5 DDD 1 1
So that dummy = 1, wherever CategoryA == special
I tried searching for this, but I suspect it requires %in% which is hard to google for.
I may be doing this for misguided reasons, so maybe you can help me short-circuit the problem—my objective in creating this dummy variable is to create a bar graph that’s sort of a sideways histogram listing all the categories by count. I want this variable to provide a group in order to color the ones where dummy==1 red and the ones where dummy==0 green. I’ll be using ggplot2.
Ugh, just after posting, I figured this out from someone answering an earlier question of mine…
For completeness, I also had to add this to my ggplot code…