missatomicbomb
missatomicbomb

Reputation: 31

Stuck on making a dummy variable in R

I am trying to create a dummy variable if two column criteria are met but its not working. So for example, in my data I want the dummy variable to be 1 if the firm_state is CA, MA, MD, ME...etc and my State is CA, MA, MD, ME, etc.. So in my picture I would want the lines that have the State as MD or ME and the firm_state as CA to have dummy variable "1" and the others that have State as AZ or TX and firm_state as CA to have "0". However, when I write my code, I just have everything in same_party as having a dummy variable "1". Can someone please tell me where I went wrong? This is my current code.

data <- data %>%
mutate(same_party = ifelse(firm_state == "CA" | firm_state == "CO" | 
firm_state == "NY" & State == "MD" | 
State == "ME" | State == "WA", 1, 0))

enter image description here

Upvotes: 0

Views: 136

Answers (2)

Ricardo Semi&#227;o
Ricardo Semi&#227;o

Reputation: 4456

You can use %in% instead of lots of ==:

data <- data %>%
  mutate(same_party = ifelse(firm_state %in% c("CA","CO","NY") &
                             State %in% c("MD","ME","WA"),1,0))

Dummy data:

data = data.frame(
  State = sample(c("AZ","LA","MD","ME","MD","WA"),10, TRUE),
  firm_state = sample(c("CA","CO","NY"), 10, TRUE))

Output:

   State firm_state same_party
1     WA         CA          1
2     MD         CO          1
3     MD         CA          1
4     LA         CA          0
5     MD         NY          1
6     ME         NY          1
7     MD         NY          1
8     MD         NY          1
9     LA         NY          0
10    ME         NY          1

Upvotes: 2

DPH
DPH

Reputation: 4344

The problem seems to be that your conditions are not well separated or grouped. Meaning that you have chain of "or" with one "and" in the middle. Using to pais of brackets should solve your problem:

data <- data %>%
  mutate(same_party = ifelse((firm_state == "CA" | firm_state == "CO" | 
                               firm_state == "NY") & (State == "MD" | 
                               State == "ME" | State == "WA"), 1, 0))

A bit more comprehensible would be the use of the %in% operator:

data <- data %>%
  mutate(same_party = ifelse(firm_state %in% c("CA","CO","NY") & State %in% c("MD","ME","WA"), 1, 0))

Upvotes: 2

Related Questions