Skip to content

how to rename variables by lopping off first 3 characters

8 messages · Christopher W. Ryan, Stefan Schreiber, Bert Gunter +3 more

#
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
#
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:

            

  
  
#
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)
i..One i..Two ixx
1      1      a   5
2      2      b   6
3      3      c   7
+              substring(nm,4),
+              nm)
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 at r-project.org> wrote:
#
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 at gmail.com> wrote:

            

  
  
#
To reply to Heinz's question:
Purely to avoid regex's, which I thought many would view as complex
and mysterious. But I am glad that you posted the regex approach -- I
would agree that it is more elegant, albeit opaque to those unfamiliar
with regex's.

Bert Gunter

On Mon, Mar 14, 2022 at 10:36 AM Christopher W Ryan via R-help
<r-help at r-project.org> wrote:
#
Hello,

I have already seen those names starting with 'i..' several times but I 
don't remember where nor why.
I think it has to do with encoding. Or maybe special characters. I have 
the impression that the best solution was to read the data in a way as 
to avoid the problem.

Using Bert's example data set, here are base R and tidyverse solutions.


dat <- data.frame(
   i..One = 1:3,
   i..Two = letters[1:3],
   ixx = 5:7)

# base R
names(dat) <- sub("^i\\.\\.", "", names(dat))
names(dat)

# tidyverse
dplyr::rename_with(dat, .fn = \(x) sub("^i\\.\\.", "", x), 
starts_with("i.."))


Hope this helps,

Rui Barradas

?s 16:26 de 14/03/2022, Christopher W Ryan via R-help escreveu:
#
The dots may appear because you don't use check.names=FALSE with
read.table().  E.g.,
header=TRUE)
  Temp...C. Length..cm.
1      37.8         145
header=TRUE, check.names=FALSE)
  Temp (?C) Length (cm)
1      37.8         145

-Bill
On Mon, Mar 14, 2022 at 10:57 AM Rui Barradas <ruipbarradas at sapo.pt> wrote:

            

  
  
#
If the phrase "some of the variable names begin with" actually turns out to be a sloppy version of "the first column name begins with", then the best solution is probably to specify fileEncoding="UTF-8-BOM" as an argument to read.csv before trying all this fix-after-the-fact work. This will also handle other non-ASCII characters properly if they are present.
On March 14, 2022 10:52:49 AM PDT, Rui Barradas <ruipbarradas at sapo.pt> wrote: