I have an existing dataset that contains zip codes for each participant. I am trying to add relevant demographic data for each zip code to the set and running into issues.
I was looking at a loop but did not fully understand it. I've started using if_else but that's also not quite working for what I'd like to do.
Essentially what I need to do is add a column for the relevant demographic, and fill the column with values corresponded to already existing values.
As an example, for the zipcode 25911, 0.857 of the population is white. In zipcode 41041, 0.952 of the population is white. And then there are a lot more to follow... I have 37 zip codes with 9 values to enter for each zip code.
Here's what I'm working with so far:
data$White <- if_else(data$Zipcode == 25911, 0.857, 0,
if_else(data$Zipcode ==41041, 0.952, 0,
if_else(data$Zipcode ==41042, 0.917, 0)))
I know that the 0 at the end tells it to fill in a 0 for every other value, but can I get it to essentially stack and build? So that first round, fill in the value for White every time the zip code is 25911, second time fill in the value for White every time the zip code is 41041, etc. Should I try a loop instead?
All help is greatly appreciated! I'm still very new to R and understanding how to best use it.