We extract the digits with parse_number
, subtract 1, and paste
with the 'names'
df1$names <- with(df1, paste0(names, '-', readr::parse_number(names) -1))
df1$names
#[1] "S1_xxx-1-0" "S1_xxx-1-0" "S1_xxx-1-0" "S2_xxx-1-1" "S2_xxx-1-1" "S2_xxx-1-1"
Or similar option in tidyverse
library(dplyr)
library(stringr)
df1 %>%
mutate(names = str_c(names, readr::parse_number(names), sep = '-'))
data
df1 <- structure(list(names = c("S1_xxx-1", "S1_xxx-1", "S1_xxx-1",
"S2_xxx-1", "S2_xxx-1", "S2_xxx-1"), value1 = c(9.65, 1.15, 3.05,
7.12, 8.9, 4.23), Value2 = c(1.24, 3.64, 1.65, 6.109, 6.03, 2.1
)), class = "data.frame", row.names = c(NA, -6L))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…