Help merging two data frames by a new column that doesn't share the same name

Hi JackDavision and Andresrcs

thanks for the help! i have another question :slight_smile: say that I have 4 data frames in R and i want to "map and merge them some of the columns from different df.

df1 = data.frame(OPER = c(1,2,5,6) , LRM = c(11,12,13,14))
df2 = data.frame(CODE = c(5,1,8,9), DUN = c(112,113,114,115)

i need to add map and merge (if that makes sense) OPER to df2 so that the cell from OPER that is = 1 is matched with the cell from CODE = 1.

i have tried the merge function from R without much luck

thanks

I'd use dplyr for that.

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df1 = tibble(OPER = c(1,2,5,6), LRM = c(11,12,13,14))
df2 = tibble(CODE = c(5,1,8,9), DUN = c(112,113,114,115))

df1 |> 
  rename(CODE = OPER) |> 
  full_join(df2)
#> Joining, by = "CODE"
#> # A tibble: 6 x 3
#>    CODE   LRM   DUN
#>   <dbl> <dbl> <dbl>
#> 1     1    11   113
#> 2     2    12    NA
#> 3     5    13   112
#> 4     6    14    NA
#> 5     8    NA   114
#> 6     9    NA   115

Created on 2021-12-06 by the reprex package (v2.0.1)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.