ogawa ogawa - 17 days ago 6
R Question

rename column values using another data

I want to rename column data.

for example

I have under dataset.

change_name <- data.frame(org_name = c("a", "b", "c"), new_name = c("A", "B", "C"))
dt <- data.frame(name = c("a", "b", "c"), data = c(1, 2, 3))


dt
dataset column
name
change using
change_name
dataset

and want to get like the under dataset.

dt <- data.frame(name = c("A", "B", "C"), data = c(1, 2, 3))

Answer

We can use match

dt$name <- change_name$new_name[match(dt$name, change_name$org_name)]
Comments