2

I have a large list with more than 100 elements. I would like to add 200 to the ID.

var <- c("ID66Session1_Date-2017-02-28__10-38.csv","ID66Session2_Date-2017-03-04__21-31.csv","ID66Session3_Date-2017-03-07__19-17.csv",
         "ID66Session4_Date-2017-03-09__18-08.csv","ID66Session5_Date-2017-03-15__16-26.csv","ID66Session6_Date-2017-04-15__23-39.csv",
         "ID6Session1_Date-2017-01-20__11-06.csv")

aim <- c("ID266Session1_Date-2017-02-28__10-38.csv","ID266Session2_Date-2017-03-04__21-31.csv","ID266Session3_Date-2017-03-07__19-17.csv",
         "ID266Session4_Date-2017-03-09__18-08.csv","ID266Session5_Date-2017-03-15__16-26.csv","ID266Session6_Date-2017-04-15__23-39.csv",
         "ID206Session1_Date-2017-01-20__11-06.csv")

I believe it's possible to solve this with grepl. But other solutions are also welcomed. I am looking for a handy and short code.

2
  • What is the range of the original ID's? Maybe from 1 to 99? Commented Dec 4, 2018 at 12:51
  • The range is 1 to 150 Commented Dec 4, 2018 at 12:52

2 Answers 2

2

gsubfn is exactly for that:

library(gsubfn)
gsubfn("((?<=ID)\\d+)", function(x, ...) as.numeric(x) + 200, var, perl = TRUE)
# [1] "ID266Session1_Date-2017-02-28__10-38.csv" "ID266Session2_Date-2017-03-04__21-31.csv"
# [3] "ID266Session3_Date-2017-03-07__19-17.csv" "ID266Session4_Date-2017-03-09__18-08.csv"
# [5] "ID266Session5_Date-2017-03-15__16-26.csv" "ID266Session6_Date-2017-04-15__23-39.csv"
# [7] "ID206Session1_Date-2017-01-20__11-06.csv"

As to avoid constructing a replacement value by "ID" + (200 + ID value) + "Session...", we use a positive lookbehind: If there is "ID" right behind us, we take all the subsequent digits. Those are then converted to a number and 200 is added.

Sign up to request clarification or add additional context in comments.

Comments

1

Here is a solution using stringr and purrr

library(stringr)
library(purrr)
map_chr(map(str_split(var, "(?<=ID)"), function(x)
    unlist(str_split(x, "(?=Session)"))), function(y)
        paste0(y[1], as.numeric(y[2]) + 200, y[3]))
#[1] "ID266Session1_Date-2017-02-28__10-38.csv"
#[2] "ID266Session2_Date-2017-03-04__21-31.csv"
#[3] "ID266Session3_Date-2017-03-07__19-17.csv"
#[4] "ID266Session4_Date-2017-03-09__18-08.csv"
#[5] "ID266Session5_Date-2017-03-15__16-26.csv"
#[6] "ID266Session6_Date-2017-04-15__23-39.csv"
#[7] "ID206Session1_Date-2017-01-20__11-06.csv"

Explanation: We split var into three parts using a positive look-behind and look-ahead: part 1 ending in ID, part 2 between (and excluding) ID and Session (i.e. the relevant part containing the ID number), and part 3 starting with Session. We then convert the middle part as.numeric and add 200 before concatenating all three parts.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.