ogawa ogawa - 1 year ago 74
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))

dataset column
change using

and want to get like the under dataset.

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

Answer Source

We can use match

dt$name <- change_name$new_name[match(dt$name, change_name$org_name)]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download