0

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))
1
  • You could make change_name into a lookup table dt$name <- setNames(change_name$new_name, change_name$org_name)[dt$name] or if you literally just need to capitalize, use toupper. Commented Nov 18, 2016 at 3:37

2 Answers 2

1

We can use match

dt$name <- change_name$new_name[match(dt$name, change_name$org_name)]
Sign up to request clarification or add additional context in comments.

2 Comments

I want to use under the case, too. change_name <- data.frame(org_name = c("a", "b", "c"), new_name = c("honda", "BMW", "toyota"))
@ogawa My solution is based on the question you asked i.e. dt dataset column name change using change_name dataset
0

I resolve the problem use under the code, too.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.