I have a group of datasets, from a survey applied to many different countries, which I want to combine to create a single merged data.frame. Unfortunately, for one of them , the variable names is different from the others, but it follows a pattern: as in the others the names of the variables are like "VAR1", "VAR2", etc., in this one their names are "VAR_a", "VAR_b", etc.
The code I've used so far to solve this problem is something like:
names (df) <- gsub("_a", "01", names(df))
names (df) <- gsub("_b", "02", names(df))
names (df) <- gsub("_c", "03", names(df))
names (df) <- gsub("_d", "04", names(df))
names (df) <- gsub("_e", "05", names(df))
names (df) <- gsub("_f", "06", names(df))
names (df) <- gsub("_g", "07", names(df))
up to the 14th letter/ number (no variable goes further than that), so that it can become similar to the other data.frames.
I know there should be a way of doing that with a few or maybe even one single line of code, but I can't find a way to do an iteration or any argument inside gsub itself to do this. Can anyone help me?
I was thinking maybe about something like:
names (df) <- gsub ("_[a-z]", "[1-9]", names(df))
But this didn't work, of course. I need R to understand I want each letter to become the corresponding number ("_a" becomes 1, etc.)
Appreciate any help.