Avi Gross
2022-Mar-15 01:21 UTC
[R] how to rename variables by lopping off first 3 characters
As R packages are frowned on by some, may I suggest a rather trivial base R solution like: names(mydf) <- sub("^i[.][.]", "", names(mydf)) To be clear, the request is not about general R variable but about the names of columns in a data.frame or tibble so all you need to do in your simple scenario is make some regular expression that matches only you specified string of "i.." at the beginning of each word and then use sub() to replace it with nothing. Give it an argument that contains the current names in your data.frame and save the selectively shortened results as the new names. And, yes, dplyr keeps evolving ever more general ways to do things and those are fine too. Of course not all changes of this kind can be made by finding a regular expression. -----Original Message----- From: Stefan Schreiber <sschreib at ualberta.ca> To: Christopher W Ryan <cryan at binghamton.edu> Cc: R-help <R-help at r-project.org> Sent: Mon, Mar 14, 2022 1:07 pm Subject: Re: [R] how to rename variables by lopping off first 3 characters This should work (couldn't check - not at my computer right now): my_df %<% rename_at(vars(starts_with("i..")), funs(str_replace(., "i..", ""))) There are also a lot of tutorials on the internet. Google something like "rename_at str_replace" HTH, Stefan On Mon, Mar 14, 2022, 10:27 Christopher W Ryan via R-help, < r-help at r-project.org> wrote:> I have data coming to me from another source, in which some of the variable > names begin with "i.." > > As in "i..actual_meaningful_var_name" > > I would like to remove the first three characters from any variable name if > they are "i.." > > I'm using R on Win 10 and dplyr, so ideally I'm looking for a dplyr > solution. Apparently I'm just not understanding how the various select, > contains, rename_at, rename_with, and so-on dplyr expressions work. I've > tried various arrangements of them, usually resulting in > > Error: `contains()` must be used within a *selecting* function. > i See <https://tidyselect.r-lib.org/reference/faq-selection-context.html> > > A simple select(contains(foo)) I can do fine, to select a subset of > variables. It's combining it with renaming that I am struggling with. > > Grateful for any advice. > > Thanks. > > --Chris Ryan > >? ? ? ?? [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.>??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.