Amaranta_Remedios
Amaranta_Remedios

Reputation: 773

Modify colnames in R composed of string and number

I have a big df with colnames like this:

dput(head(colnames(count)[c(2,3,4,7,8)]))
c("A001", "A002", "A004", "A008", "A009")

I want to substract the number part and keep the letter, which is not a constant string and in other columns it will be B, C etc. result should look like this:

c("A000", "A001", "A003", "A007", "A008")

So far I was trying this which deals with the number -1 but it doesn't keep the letter.

as.numeric(str_extract(colnames(count), "[0-9]+"))-1
c("0", "1", "3", "7", "8")

Upvotes: 3

Views: 63

Answers (3)

akrun
akrun

Reputation: 887128

Using str_replace which can take a function as well

library(stringr)
str_replace(vec, "\\d+", function(x) sprintf('%03d', as.numeric(x) - 1))
[1] "A000" "A001" "B003" "A007" "D008"

data

vec <- c("A001", "A002", "B004", "A008", "D009")

Upvotes: 3

s_baldur
s_baldur

Reputation: 33488

One base R option:

x = c("A001", "A002", "B004", "C008", "D009")
sapply(
  strsplit(x, "(?<=[A-Z])", perl = TRUE),
  \(x) sprintf("%s%03d", x[1], as.numeric(x[2])-1)
)
# [1] "A000" "A001" "B003" "C007" "D008"

Upvotes: 4

Ma&#235;l
Ma&#235;l

Reputation: 52004

You can use gsub to extract the numbers and letter and operate on them, then sprintf to format your string:

vec <- c("A001", "A002", "B004", "A008", "D009")

sprintf("%s%03d", gsub("\\d", "", vec), as.integer(gsub('\\D', "", vec)) - 1)
# [1] "A000" "A001" "B003" "A007" "D008"

Upvotes: 3

Related Questions