I have the following data.frame file:
set.seed(126) df <- data.frame(a=sample(c(1:100, NA), 10), b=sample(1:100, 10), c=sample(1:100, 10), d = c(1:10)) abcd 1 18 27 53 1 2 44 16 66 2 3 58 47 3 3 ...
And the following lookup table:
varnames <- data.frame(old = c("a", "b", "c"), new = c("dog", "cat", "mouse")) old new 1 a dog 2 b cat 3 c mouse
What I'm trying to do is replace names(df) with the corresponding varnames$new ... If a names(df) not in varnames$old , save colname in df ...
The resulting data.frame file that I would like to return would look like this:
dog cat mouse d 1 57 10 83 1 2 53 99 94 2 3 99 60 39 3 ...
Any help is appreciated.
source share