0

I am very new to the language R. I have a simple problem I can't seem to solve: I have two 2D data sets called airlines and flights. I want to substitute the "two letter carrier abbreviation" in the data frame flights by the full name that you can find in the data frame airlines. In the airlines data frame the abbreviation and the full name have the same row, but not column.

Can anyone give me any advice? I have been trying to put it in a for loop and use the function gsub() but haven't solved it yet. Any help would be a lifesaver!

montrealist
  • 5,593
  • 12
  • 46
  • 68
  • 3
    Hi and welcome to SO. Can you share a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) that illustrates your problem? Also show expected output. – markus Oct 09 '18 at 18:26
  • 2
    Possibly related: [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) – markus Oct 09 '18 at 18:28

2 Answers2

1
library(dplyr)
combo <- flights %>%
 left_join(airlines)
Jon Spring
  • 55,165
  • 4
  • 35
  • 53
0
newdf <- merge(x = flights,
               y = airlines,
               by.x = 'two_letter_carrier_abbreviation', #abbreviation column from flights
               by.y = 'two_letter_carrier_abbreviation', #abbreviation column from airlines
               all.x = TRUE,  #keep all rows in flights
               all.y = FALSE) #don't add full names that are not found in flights

newdf$two_letter_carrier_abbreviation <- NULL #remove the abbreviation column
12b345b6b78
  • 995
  • 5
  • 16