0

Is it possible to create column labels while piping in R with dplyr? E.g. something like:

df <- mtcars %>% 
  some_lovely_function_for_labels(mpg = 'Miles per gallon')

Perhaps the solution could work for a list of variable-labels pairs.

I've explored the labelled package to no avail.

Brendan
  • 166
  • 1
  • 4
  • 1
    Are you looking for `rename` and `rename_with` of {dplyr} perhaps? – I_O May 26 '23 at 14:27
  • Something like that, but for variable labels rather than variable names. I know labels are not often used in R, but I have imported a dataset from SAS and would like to be able to manipulate what came in. – Brendan May 26 '23 at 14:32
  • I see. AFAIK, R has no concept of variable labels (beyond names), but see here: https://stackoverflow.com/questions/27347548/r-assign-variable-labels-of-data-frame-columns and here: https://stackoverflow.com/questions/65317041/how-to-get-variable-name-and-labels-from-sas7bdat-into-a-data-frame (or here for a GUI-orientated software with R under the hood: https://www.jamovi.org/) – I_O May 26 '23 at 14:39

1 Answers1

1

Followed the link provided by I_O and learned about the package Hmisc. The following seems to work:

df <- mtcars %>% 
  Hmisc::upData(labels = c(mpg = 'Miles per gallon'))
Brendan
  • 166
  • 1
  • 4