Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6926625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:56:33+00:00 2026-05-27T10:56:33+00:00

What would be an efficient way of replacing a fixed position substring with another

  • 0

What would be an efficient way of replacing a fixed position substring with another string of equal or larger length?

For example, the following replaces the substring “abc” by finding the position of “abc” first and then replacing it:

sub("abc", "123", "iabc.def", fixed = TRUE)
#[1] "i123.def"

sub("abc", "1234", "iabc.def", fixed = TRUE)
#[1] "i1234.def"

However, we know that the substring “abc” is ALWAYS in character positions 2, 3 and 4. In this case, is there a way of specifying those positions so that the string matching doesn’t need to be performed and the character indices used instead?

I did try to use substr() but it didn’t work as I had hoped when the replacement string is larger than the substring being replaced:

x <- "iabc.def"
substr(x, 2, 4) <- "123"
#[1] "i123.def"

x <- "iabc.def"
substr(x, 2, 4) <- "1234"
#[1] "i123.def"

Many thanks in advance for your time,

Tony Breyal

P.S. The above may be the most efficient way of doing what I want but I thought I would ask just in case there is a better way 🙂

===== TIMINGS =====

#                             test elapsed  relative
# 7 francois.fx_wb(x, replacement)    0.94  1.000000
# 1                           f(x)    1.56  1.659574
# 6    francois.fx(x, replacement)    2.23  2.372340
# 5                      Sobala(x)    3.89  4.138298
# 2                    Hong.Ooi(x)    4.41  4.691489
# 3                        DWin(x)    5.57  5.925532
# 4                      hadley(x)    9.47 10.074468

The above timings were generated from the code below:

library(rbenchmark)
library(stringr)
library(Rcpp)
library(inline)

f <- function(x, replacement = "1234")  sub("abc", replacement, x, fixed = TRUE)

Hong.Ooi <- function(x, replacement = "1234") paste(substr(x, 1, 1), replacement, substr(x, 5, nchar(x)), sep = "")

DWin <- function(x, replacement =  paste("\\1", "1234", sep = "")) sub("^(.)abc", replacement, x)

Sobala <- function(x, replacement =  paste("\\1", "1234", sep = ""))  sub("^(.).{3}", replacement, x, perl=TRUE)

hadley <- function(x, replacement = "1234") {
  str_sub(x, 2, 4) <- replacement
  return(x)
}

francois.fx <- cxxfunction( signature( x_ = "character", rep_ = "character" ), '

    const char* rep =as<const char*>(rep_) ;
    CharacterVector x(x_) ;
    int nrep = strlen( rep ) ;
    int n = x.size() ; 
    CharacterVector res(n) ;

    char buffer[1024] ;

    for(int i=0; i<n; i++) {
        const char* xi = x[i] ;
        if( strncmp( xi+1, "abc", 3 ) ) {
            res[i] = x[i] ;
        } else{
            buffer[0] = xi[0] ;
            strncpy( buffer + 1, rep, nrep ) ;
            strcpy( buffer + 1 + nrep, xi + 4 ) ;
            res[i] = buffer ;
        }
    }
    return res ;
', plugin = "Rcpp" )

francois.fx_wb <- cxxfunction( signature( x_ = "character", rep_ = "character" ), '

    const char* rep =as<const char*>(rep_) ;
    int nrep = strlen( rep ) ;
    int n=Rf_length(x_) ;
    SEXP res = PROTECT( Rf_allocVector( STRSXP, n ) ) ;

    char buffer[1024] ;

    for(int i=0; i<n; i++) {
        const char* xi = char_get_string_elt(x_, i) ;
        if( strncmp( xi+1, "abc", 3 ) ) {
            set_string_elt( res, i, get_string_elt(x_,i)) ;
        } else{
            buffer[0] = xi[0] ;
            strncpy( buffer + 1, rep, nrep ) ;
            strcpy( buffer + 1 + nrep, xi + 4 ) ;
            char_set_string_elt(res, i, buffer ) ;
        }
    }
    UNPROTECT(1) ;
    return res ;
', plugin = "Rcpp" )


x <- rep("iabc.def", 1e6)
replacement <- "1234"
benchmark(f(x), Hong.Ooi(x), DWin(x), hadley(x), Sobala(x), francois.fx(x, replacement), francois.fx_wb(x, replacement),
          columns = c("test", "elapsed", "relative"),
          order = "relative",
          replications = 10)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:56:34+00:00Added an answer on May 27, 2026 at 10:56 am

    Here is one solution based on Rcpp.

    fx <- cxxfunction( signature( x_ = "character", rep_ = "character" ), '
    
        const char* rep =as<const char*>(rep_) ;
        CharacterVector x(x_) ;
        int nrep = strlen( rep ) ;
        int n = x.size() ; 
        CharacterVector res(n) ;
    
        char buffer[1024] ;
    
        for(int i=0; i<n; i++) {
            const char* xi = x[i] ;
            if( strncmp( xi+1, "abc", 3 ) ) {
                res[i] = x[i] ;
            } else{
                buffer[0] = xi[0] ;
                strncpy( buffer + 1, rep, nrep ) ;
                strcpy( buffer + 1 + nrep, xi + 4 ) ;
                res[i] = buffer ;
            }
        }
        return res ;
    ', plugin = "Rcpp" )
    

    it does not improve much on the simple sub solution because write access to strings in R are protected by the write barrier. I get better results if I cheat on the write barrier, but I’m not fully aware of the consequences, so I should probably advise against it :/

    fx_wb <- cxxfunction( signature( x_ = "character", rep_ = "character" ), '
    
        const char* rep =as<const char*>(rep_) ;
        int nrep = strlen( rep ) ;
        int n=Rf_length(x_) ;
        SEXP res = PROTECT( Rf_allocVector( STRSXP, n ) ) ;
    
        char buffer[1024] ;
    
        for(int i=0; i<n; i++) {
            const char* xi = char_get_string_elt(x_, i) ;
            if( strncmp( xi+1, "abc", 3 ) ) {
                set_string_elt( res, i, get_string_elt(x_,i)) ;
            } else{
                buffer[0] = xi[0] ;
                strncpy( buffer + 1, rep, nrep ) ;
                strcpy( buffer + 1 + nrep, xi + 4 ) ;
                char_set_string_elt(res, i, buffer ) ;
            }
        }
        UNPROTECT(1) ;
        return res ;
    ', plugin = "Rcpp" )
    

    Write barrier

    The R Internals manual describes the write barrier:

    A generational collector needs to efficiently ‘age’ the objects,
    especially list-like objects (including STRSXPs). This is done by
    ensuring that the elements of a list are regarded as at least as old
    as the list when they are assigned. This is handled by the functions
    SET_VECTOR_ELT and SET_STRING_ELT, which is why they are functions and
    not macros. Ensuring the integrity of such operations is termed the
    write barrier and is done by making the SEXP opaque and only providing
    access via functions (which cannot be used as lvalues in assignments
    in C).

    All code in R extensions is by default behind the write barrier.

    And Luke Tierney’s document describes the logic behind why:

    The generational collector divides allocated nodes into generations
    based on some notion of age. Younger generations are collected more
    frequently than older ones. For this to work correctly, any younger
    nodes that are reachable only from older nodes must be handled
    properly. This is accomplished by a write barrier that monitors each
    assignment and takes appropriate action when a reference to a new node
    is placed in an older one.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what would be the efficient way of saving the following array using php (cakephp)?
Consider the string: string str=A C# string; What would be most efficient way to
Would the following be the most efficient way to get an int16 (short) value
What would be an efficient way of converting a delimited string into an array
What would be the most efficient way of recording to a log (.txt) from
What would be the most efficient way of counting the number of times a
In SQL Server would a varbinary(16) be the most efficient way of storing an
If I have two associative arrays, what would be the most efficient way of
What would be a efficient way to generate all possible IP v4 addresses? other
What is the most efficient way of retrieving and replacing the last line of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.