Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keeping column labels when using the select function with dplyr

Tags:

r

dplyr

I'm combining several datasets and keeping only specific columns, however, when I use the select function from dplyr, it does not retain the original column labels (in my case, the item text).

After I import the data, I use the following code to select the variables I'd like to keep:

Wave1Data_Clean <- select(Wave1Data, ID, x1, x2, x3, x4, x5)

When I do this, the column names are correct as are the values, however, the column labels are replaced with the labels of the original dataframe. For example, if column #1's label was 'location' in the original data, the column name for the ID in the df would be 'location' instead of the label associated with ID.

Is there a way to retain the appropriate column labels?

like image 756
Chris Wiese Avatar asked May 29 '26 00:05

Chris Wiese


1 Answers

I can see the problem you describe when I view the data frame in RStudio.

Example_Clean in RStudio

I'm not sure why the labels get mixed up, but you can overwrite them with the correct labels.

Assuming that you're using read.spss from the foreign package, the labels are stored as an attribute called "variable.labels". You can access the labels using:

attr(Example_Clean, "variable.labels")

The following code overwrites the labels with the correct ones.

var_labs <- attr(Example_Clean, "variable.labels")
var_labs <- var_labs[names(Example_Clean)]
attr(Example_Clean, "variable.labels") <- var_labs
like image 62
Kelly Avatar answered May 31 '26 05:05

Kelly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!