ogw
ogw

Reputation: 373

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))

Upvotes: 0

Views: 63

Answers (2)

ogw
ogw

Reputation: 373

I resolve the problem use under the code, too.

dt$name <- 
  plyr::mapvalues(dt$name, change_name$org_name, change_name$new_name

Upvotes: 0

akrun
akrun

Reputation: 887118

We can use match

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

Upvotes: 1

Related Questions