I am trying to use conditionals to assign a new variable based on variables from two different data frames. The variable I'm trying to use is Zipcode
Data frame A contains 2 Variables: Zipcode, population and state.
Data frame B contains all US Zipcodes and obesity rate.
I want data frame A to have a new variable: obesity rate according to zipcode and state for Texas only, assigning all other states a 0.
A
Zipcode | Population | State
33333 | 700 | Texas
11111 | 600 | Oregon
77777 | 500 | Texas
66666 | 100 | Texas
B
Zipcode obesity
11111 | 1.4
22222 | 2.2
33333 | 1.12
44444 | 3.33
55555 | 1.3
66666 | 2
77777 | 5
Here is my code so far:
A$obesity <- ifelse((A$Zipcode == B$Zipcode) & (A$state == "Texas"), B$obesity, 0)
This often assigns Texas an obesity but not all values of texas receive an obesity and i get the error:
In which(A$Zipcode == B$Zipcode) & (A$state == : longer object length is not a multiple of shorter object length