larmarange / labelled

Manipulating labelled vectors in R
https://larmarange.github.io/labelled/
GNU General Public License v3.0
75 stars 16 forks source link

Feature requests: Simple manipulations of variable labels #153

Closed sda030 closed 7 months ago

sda030 commented 12 months ago

Hi Joseph, I was wondering whether something like the following would be something to include in the package? It is relatively easy to do it in a script for an intermediate R user, but I have newbie colleagues who find it a bit overwhelming/complicated to set up a loop across variable labels, etc. And cleaning variable labels are very important, as you might know, in the social sciences for effective science outreach.

Imagine this generalized solution to the common problem of wanting to generally process many variables' labels. labelled::process_var_labels(data, ..., .f = ~stringr::str_replace_all(.x, pattern = "sad socks", replacement = "Happy socks")) # returns dataframe/tibble. The elipses would preferably take tidyselect-notation. The stringr-function can of course be replaced by the user to something like str_trim, etc. Not very advanced to implement and maintain, the tidyselect you already have in the package. To ensure that stringr/stringi works as efficiently as possible, it would probably be best to first extract all the labels into a named vector, and then process them.

Let me know if you are too busy and I could try something.

P.S. Function should ignore NULL-labels, yet return the correct labels to each variable..

larmarange commented 12 months ago

Dear @sda030

Yes such a function could be considered to be added in the package.

I'm currently traveling for work reasons, but I would be happy to look at a PR.

Best regards

larmarange commented 8 months ago

Hi. Are you still interested in such feature?

What about two functions update_variable_labels_with() and update_value_labels_with() allowing to apply a function to variable / value labels. Following dplyr::rename_with(), we could consider a .cols argument to apply it only to certain columns.

These two functions will be applied only to data.frames.

sda030 commented 8 months ago

That sounds excellent Joseph! I attempted one myself but was unsatisfied with lack of vectorization. Then looked into Rust but never got around to a solution. I'm sure you can do something better than me.

larmarange commented 8 months ago

you may have a look and test #158

sda030 commented 8 months ago

Works well! This will assist my colleagues so much.