0

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

zx8754
  • 52,746
  • 12
  • 114
  • 209

1 Answers1

0

It sounds like what you actually want to do is a JOIN operation on the zipcode variable. You can do that easily in dplyr:

A %>%
    left_join(B, by = 'Zipcode')

If you're only interested in the state of texas, just add a filter condition:

A %>%
    left_join(B, by = 'Zipcode') %>%
    filter(State = 'Texas')

If you want to keep all rows, but change non-Texan rows to 0:

# Insert 0 into non-Texan rows using bracket notation
A %>%
    left_join(B, by = 'Zipcode')

A[A$State != 'Texas', 'obesity'] <- 0

or:

# Use mutate to multiply 'obesity' by a vector of whether State == Texas
#  as.numeric() of a logical vector gives a vector of 0 or 1
#  If State == Texas: 1 * obesity = obesity; otherwise: 0 * obesity = 0
A %>%
    left_join(B, by = 'Zipcode') %>%
    mutate(obesity = obesity * as.numeric(State == 'Texas'))
divibisan
  • 11,659
  • 11
  • 40
  • 58
  • Thanks! This did almost everything I wanted. Is there a way I could edit your code to keep the observations that aren't "Texas" and assign them a 0? – J Pfaff Apr 09 '18 at 15:14