Hi,
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.
Hi,
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.
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?
names(mydata)
dplyr::glimpse(mydata)
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:
https://stackoverflow.com/questions/72969805/rename-variables-with-variable-labels-in-r
I checked it and it works.
Or even simpler with one line of code:
library(labelled)
names(mydata) <- as.character(var_label(mydata))
form here:
https://stackoverflow.com/questions/71073239/changing-a-variable-attribute-into-the-variable-name
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)
Thanks so much! This worked!