0

I am not very good at R, but I am trying it out to prepare my data for Regression Analysis. So I have three Data frames, and I am trying to add a column to the first dataframe.

Essentially my data is laid out as:

City                 Neighborhood           X           Y            Z 

Euclid                 N/A
Cleveland             Glenville
Mayfield Heights       N/A
Euclid                 N/A
Euclid                 N/A
Cleveland             Clark-Fulton

and I want to add a a column titled: PercentHispanic (it doesn't matter where in the dataframe).

My second dataframe looks like:

City         PercentHispanic

Euclid        X%

Cleveland     Y%

Mayfield      Z%

And for the City of Cleveland, my dataframe looks like:

Neighborhood        PercentHispanic

Glenville              X%

Clark-Fulton           Y%

Downtown               Z%

How do I add the column PercentHispanic? I want the dataframes to be merge based on City, unless the City is Cleveland, then I want it to merege based on Neighborhood. I do not want to get rid of the N/A values (I want to keep all rows).

Thank you!

KVHelpMe
  • 81
  • 5
  • 2
    Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – Eric Leung Oct 03 '20 at 18:27
  • Save all data frames in a list and use answers here: [Simultaneously merge multiple data.frames in a list](https://stackoverflow.com/q/8091303/1422451). – Parfait Oct 03 '20 at 19:03

1 Answers1

0

We can do two joins

library(dplyr)
out <- left_join(df1, df2, by = 'City') %>%
   left_join(df3, by = 'Neighborhood')
akrun
  • 874,273
  • 37
  • 540
  • 662