I have two dataframes, DF1 and DF2:
DF1 <- data.frame(V1 = factor(c("A", "B", "C", "D")),
V2 = factor(c("E", "F", "G", "H")),
Va3 = factor(c("I", "J", "K", "L")),
column = factor(c("M", "N", "O", "P")))
DF2 <- data.frame(N1 = factor(c("x", "V1", "V2", "y", "z", "Va3", "a", "column")),
N2 = factor(c("A", "var1", "random", "R", "Q", "nameofcolumn", "S", "varname4")))
I want to change the name of variables in DF1 (V1:column) based on the value of the corresponding cell in DF2$N2, so that, e.g. V2 becomes random and column becomes varname4.
Normally, I would just use colnames(DF1) <- DF2$N2 if the variable names in DF1 matched the cell values in DF2; but here I have those additional values. How can I rename the variables properly?