I have a dataset in R where the first row contains labels for each column. Is there a way to assign this first row as column name labels?
Asked
Active
Viewed 120 times
0
-
`colnames(dat) <- dat[1,]` – r2evans Nov 16 '20 at 17:47
-
3Realize, though, that assuming those are strings, then all of your data is likely string as well, even if you intend otherwise. If you have numeric values in any of the of the columns, then you'll need to repair them. However, better yet: fix the import process so that you don't have to reassign the column names, and the column types are correct up-front. – r2evans Nov 16 '20 at 17:48
-
Thank you so much! This looks like it changed the entire column name, rather than provide additional information on what the column is referring to. Is there a way to just add a label, thatcher than change the column name? – Ben Nov 16 '20 at 17:53
-
Not in base R, but certain packages add that flexibility. `qualtRics` does it automagically for Qualtrics surveys. `Hmisc` also has it, see [this possible duplicate for details.](https://stackoverflow.com/a/27347686/903061) – Gregor Thomas Nov 16 '20 at 18:05
-
You asked how to *"assign this first row as column name"*, so I went with that. As for *"label"*, please explain what you mean. It would really help if this question were reproducible. – r2evans Nov 16 '20 at 18:05
-
This fixed it. data <- labelled::set_variable_labels(data, .labels = var.labels) – Ben Nov 16 '20 at 18:07