0

I have a df for which I'd like to add one specific column from another df.

df1 <- data.frame(
    firm_id = c (1:5), 
    firm_name = c("Firm_A", "Firm_B", "Firm_C", "Firm_D", "Firm_E"),
    firm_state = c("state1", "state2", "state3", "state4", "state5"), 
    stringsAsFactors = FALSE)
print(df1) 


df2 <- data.frame(
 industry_average = c(60.3,55.2,67.4,70.9,56.15), 
 firm_name = c ("Firm_A","Firm_B","Firm_C","Firm_D","Firm_E"),
 firm_state = c("state1","state2","state3","state4","state5"), 
 stringsAsFactors = FALSE)
print(df2)

I need to join industry_average with df1 based on identical/matching rows of firm_name & firm_state in df1 & df2. I tried left join etc. but these won't work out.

Thanks in advance!

Kim Ida
  • 5
  • 3
  • What did you try? `dplyr::left_join(df1, df2, by=c("firm_name", "firm_state"))` works as expected. – Edward Apr 20 '20 at 00:31

1 Answers1

1

I think inner_join should do the trick:

df3<-df1 %>% inner_join(df2)
Tim Assal
  • 677
  • 6
  • 15
  • I tried both versions, but I get NAs in some of the rows; the 2nd df is much longer, but that shouldn't be an issue (I am a newbie so sorry for bothering) - I'm happy for any advice! – Kim Ida Apr 20 '20 at 08:39
  • Glad it worked Kim. If this answers your question satisfactorily you are encouraged to click the check-mark to accept it. – Tim Assal Apr 23 '20 at 15:14