Renaming columns by column labels


I'd like to know if there is any method to rename all the columns in a dataframe by the column labels. There is a package to that in Stata, but I couldn't find any in R.

Please let me know if you know of any such package in R.

Thank you.

I'm not exactly sure what you mean by column labels, but here is a link to renaming columns using rename() and rename_with() from the dplyr package. If you have a specific example in mind, please pass it along.

Hi, Scotty,

I am sorry I didn't give an example of what I mean before. Please see the screenshot for reference. For example, the first column is named 'v001', but it is labelled as 'cluster number'. And my goal is to rename the column same as the label.

Thank you for the screenshot. When you read this data into R (let's say as mydata), what do you get with the following two calls?



I think you may want to look at what R calls labels. Have a look at Variable and value labels support in base R and other packages and Introduction to labelled for some information. I have not used labels in years so these may or may not be good references for what you want.

There is a solution in here:

I checked it and it works.

Or even simpler with one line of code:

names(mydata) <- as.character(var_label(mydata))

form here:


I added another line to remove the variable labels once they have replaced the original variable names. It just seems neater that way.

mydata <- read_sav("test.sav")
names(mydata) <- as.character(var_label(mydata))
mydata <- remove_var_label(mydata)

1 Like

Thanks so much! This worked!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.