-3

I have two data frames of different lengths with the following variables:

df1: State, County, Income, Age

df2 (the longer one): Zip.Code, State, County

So that df1 has a unique entry for each combination of State & County, and df2 can have several of those for different zip codes.

I'm trying to assign income and age from df1 to each row (Zip.Code) in df2. I've tried:

dplyr:: left_join(df2, df1, by=c("STATE", "COUNTY"))

or

df2$Income <- NA 
df2$Age <- NA

for (i in 1:length(df2$Zip.Code)){
  for (j in 1:length(df1$STATE)){
if (df2$STATE[i]== df1$STATE[j] & df2$COUNTY[i]== df1$COUNTY[j]){
df2$Age[i] <- df1$Age[j]
df2$Income[i] <- df1$Income[j]
 }
}}

The result of this is NA for all Income and Age.

UPDATE: the problem was different ways of writing states. Thanks for the help.

Ram B
  • 157
  • 1
  • 9

1 Answers1

0

Use merge:

merge(df1,df2, by=c("County","State"))
emilliman5
  • 5,816
  • 3
  • 27
  • 37