[R] [External Email] Re: how to rename variables by lopping off first 3 characters

Christopher W Ryan cry@n @end|ng |rom b|ngh@mton@edu
Mon Mar 14 18:32:28 CET 2022


Thanks! This has gotten me on the right track.  One of my mistakes was that
I was explicitly using the word select, as in

rename_at(select(starts_with ....
or
rename_at(select(vars(starts_with ....

--Chris Ryan

On Mon, Mar 14, 2022 at 1:11 PM Bert Gunter <bgunter.4567 using gmail.com> wrote:

> If you care to entertain this, one of many simple base R ways to do this
> is:
>
> dat <- data.frame(
> +    i..One = 1:3,
> +    i..Two = letters[1:3],
> +    ixx = 5:7)
> > dat
>   i..One i..Two ixx
> 1      1      a   5
> 2      2      b   6
> 3      3      c   7
>
> > nm <- names(dat)
> > nm <- ifelse(substring(nm, 1,3) == "i..",
> +              substring(nm,4),
> +              nm)
> > names(dat) <- nm
> > dat
>   One Two ixx
> 1   1   a   5
> 2   2   b   6
> 3   3   c   7
>
> Bert Gunter
>
> "The trouble with having an open mind is that people keep coming along
> and sticking things into it."
> -- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
>
>
> On Mon, Mar 14, 2022 at 9:27 AM Christopher W Ryan via R-help
> <r-help using 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 using 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]]



More information about the R-help mailing list