Search code examples
rgsubsubstr

Change the first number in a column based on condition


I have a data frame like this:

  id subscriberid  intName
1  1   1234567890 asdfsadf
2  2   3243245324  dfsafdf
3  3   4532453245  dasdfsd

A reproducible example is as follows:

structure(list(id = 1:3, subscriberid = c(1234567890, 3243245324, 4532453245),
    intName = c("asdfsadf", "dfsafdf", "dasdfsd")),
    row.names = c(NA, 3L), class = "data.frame")

I have an array of subid and wherever the subid matches with df$subscriberid, I have to change the first digit of the subscriber id to 9.

subid = c(1234567890,2345345234)

I tried the following:

for (i in df$subscriberid) {
    df$subscriberid == sub(substr(df$subscriberid,0,1),9,df$subscriberid)
}

I have also tried with ifelse with substr and gsub, and different other combinations. But couldn't get through. The desired output is

  id subscriberid  intName
1  1   9234567890 asdfsadf   <--- only the first digit is changed.
2  2   3243245324  dfsafdf
3  3   4532453245  dasdfsd

Solution

  • One option is to use ifelse and if subscriberid is present in subid then we paste 9 with remaining string starting from 2nd index.

    df$subscriberid <- with(df, ifelse(subscriberid %in% subid,
                        paste0("9",substring(subscriberid,2)), subscriberid))
    
    df
    #  id subscriberid  intName
    #1  1   9234567890 asdfsadf
    #2  2   3243245324  dfsafdf
    #3  3   4532453245  dasdfsd
    

    The benefit of using substring is you need to only mention the start index (here 2), the default value for stop is 1000000 which covers most of the strings.