0

I have 2 data frames. One is repair cost data and the other is a list of part numbers and part names. They look like so. I need to combine the part numbers and part name into one variable in df1 so i can show both on a ggplot graph.

df1: Part_Number: c(A123, A321, A231, A231, A123, A321) Repair_Cost: c(150, 230, 100, 120, 180, 120)

df2: Part_Number: c(A321, A231, A123) Part_Name: c(Wheel, Strut, Mount)

for each observation in df1 i would like to show not only the part number, but the name associated with the part. If this can be done using tidyverse code i would appreciate that.

So far i have attempted different things this was the closest i've gotten

Df1$Part.Name<-ifelse(Df1$Part_Num %in% df2$PartNumber,df2$part_name) Df1$Part_Num_Name<-paste(Df1$Part_Num,": ",Df1$Part.Name)

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294

1 Answers1

1

You can use left_join from dplyr:

library(dplyr)

df1 <- tibble(
  Part_Number = c("A123", "A321", "A231", "A231", "A123", "A321"),
  Repair_Cost = c(150, 230, 100, 120, 180, 120)
)

df2 <- tibble(
  Part_Number = c("A321", "A231", "A123"),
  Part_Name = c("Wheel", "Strut", "Mount")
)


df1 %>% 
  left_join(df2)
#> Joining, by = "Part_Number"
#> # A tibble: 6 × 3
#>   Part_Number Repair_Cost Part_Name
#>   <chr>             <dbl> <chr>    
#> 1 A123                150 Mount    
#> 2 A321                230 Wheel    
#> 3 A231                100 Strut    
#> 4 A231                120 Strut    
#> 5 A123                180 Mount    
#> 6 A321                120 Wheel
Matt
  • 7,255
  • 2
  • 12
  • 34