0

Say I have df1 that contains names in column 1 and numerical values in the other columns, and I have df2 that contains the same set of names in column 2 and a unique corresponding identifier in column 1.

The order of the names in the dfs don't match so I need a way to replace df1 names with df2 identifiers.

I know I can do something similar with the dplyr rename function but my dataframes are HUGE so that's a lot to manually write out. I thought this could be done in base R with a simple for/if loop using logical arguments but I feel like there has to be an easier way? Any help, tips or tricks would be appreciated.

For example:

View(df1)
Name Value etc.
Heart 2 ...
Brain 5 ...
Blood 10 ...
Lung 3 ...
... ... ...
View(df2)
ID Type
H Heart
L Lung
Br Brain
Bl Blood
... ...

After code:

View(df1)
Name Value etc.
H 2 ...
Br 5 ...
Bl 10 ...
L 3 ...
... ... ...
Phil
  • 7,287
  • 3
  • 36
  • 66
Jordan
  • 11
  • 1
  • what if you just join the dataframes: `dplyr::left_join(df1, df2, by = c("Name" = "Type"))` – AndS. Sep 13 '22 at 20:40
  • Just a little FYI-- row names are a thing that are different from columns. The data you show just has columns, no row names. – Gregor Thomas Sep 13 '22 at 20:56

0 Answers0