Skip to content
Prev 391002 / 398506 Next

how to rename variables by lopping off first 3 characters

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: