0

I am looking to create a data frame in R that compiles data based on the county FIPS code. I am pulling FIPS code and county population from df1 and healthcare data from df2 (in which it is also organized by FIPS code but the FIPS codes are not in the same order as df1). I ultimately want something like this, where "x" and "z" are both unique to FIPS code "01001".

FIPS_code population healthcare
01001 x z
01003 y t

I have done: df = data.frame(df1$FIPS_code, df1$population, df2$healthcare) however this is not ensuring that my "healthcare" column is based on the same FIPS code as "population" column.

r2evans
  • 141,215
  • 6
  • 77
  • 149
  • 1
    Can you show us `str(df1)` and `str(df2)` (or `summary()`) in the question? In general, `merge()` is the right answer to this question (or `full_join()` or one of the other `*_join()` options if you're using tidyverse) – Ben Bolker May 26 '23 at 20:46
  • With data `pop <- data.frame(code=c("01001","01003","01004"),popn=c("x","y","z")); hlth <- data.frame(code=c("01001","01003"),hcare=c("z","t"))`, try `merge(pop, hlth, by="code", all=TRUE)`. See Mikko's link, plus https://stackoverflow.com/q/5706437/3358272 and https://stackoverflow.com/q/34598139/3358272. – r2evans May 26 '23 at 22:06
  • ... and while the dupe links are not asking the same question in the same way, it's the mechanism and end-result that makes this a duplicate. The premise of merge/join is a core task in data-munging, and a frequent question, please read those links (perhaps twice), and if you cannot get it to work with your data, please [edit] your question to add usable data, demonstrate with code what about `merge` does not work correctly, then @-ping me and we can sort through it. Good luck, emlightfoot! – r2evans May 26 '23 at 22:07

0 Answers0