Home > Back-end >  Rename column value based on another matching dataframe in R
Rename column value based on another matching dataframe in R

Time:01-31

Suppose that we have three dataframes dfa

name id
jack 1
rose 2

dfb

id job
a student
b tutor

dfc

primary colname
a 2
b 1

how could I rename the dfa id column from 1,2 to b,a using R?

Please help me solve this question! Thank you so much!

CodePudding user response:

One dplyr approach would be to use recode like so:

library(dplyr)

dfa |> 
  mutate(id = recode(id, !!!setNames(dfc$primary, dfc$colname)))
#>   name id
#> 1 jack  b
#> 2 rose  a

or a second option would be to use a left_join:

dfa |> 
  left_join(dfc, by = c("id" = "colname")) |> 
  select(name, id = primary)
#>   name id
#> 1 jack  b
#> 2 rose  a

DATA

dfa <- data.frame(
              name = c("jack", "rose"),
                id = c(1L, 2L)
)

dfc <- data.frame(
           primary = c("a", "b"),
           colname = c(2L, 1L)
)

CodePudding user response:

I haven't understood if the relationship between all three datasets is like this: dfa$id is equal to dfc$colname, and dfc$primary is equal to dfb$id, but if so, here's a possible answer:

# Load the dataframes
dfa <- data.frame(name = c("jack", "rose"),
                  id = c(1, 2))
dfb <- data.frame(id = c("a", "b"),
                  job = c("student", "tutor"))
dfc <- data.frame(primary = c("a", "b"),
                  colname = c(2, 1))

# Obtain the indexes that match between dfa and dfc
index <- match(dfa$id, dfc$colname)

# Set the id from dfa as the id in dfb taking into account the previous indices
dfa$id <- dfb$id[index]

# Print results
dfa

  name id
1 jack  b
2 rose  a
  •  Tags:  
  • r
  • Related