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 8827017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:16:57+00:00 2026-06-14T07:16:57+00:00

I am trying to replace characters in all elements of a string vector, where

  • 0

I am trying to replace characters in all elements of a string vector, where the characters are different, but always a certain distance from the beginning or the end of the string. I can use substr successfully to replace the characters from the beginning of the string. I am trying to use str_sub from the package stringr to replace the characters from the end of the sting (because it allows counting backwards with negative numbers). It replaces the character, but for all elements after the first one, it replaces everything to the right of the character with the end of the string from the first element:

> require(stringr)
> x <- c("A'B'C","E!FG@H","I$JKL&M")
> substr(x,2,2) <- ":"
> x
[1] "A:B'C"   "E:FG@H"  "I:JKL&M"
> str_sub(x,-2,-2) <- ":"
> x
[1] "A:B:C"   "E:FG:C"  "I:JKL:C"
  • 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-06-14T07:16:58+00:00Added an answer on June 14, 2026 at 7:16 am

    Try this:

    require(stringr)
    x <- c("A'B'C","E!FG@H","I$JKL&M")
    substr(x,2,2) <- ":"
    str_sub(x, rep(-2, length(x)),  rep(-2, length(x))) <- ":"
    

    The reason it behaves like this is because str_sub<- passes the results of str_sub(x, start, end) through str_c which is where the collapsing is going wrong for you.

    The function in the source code is:

    "str_sub<-" <- function(string, start = 1L, end = -1L, value) {
         str_c(
            str_sub(string, end = start - 1L),
            value,
            ifelse(end == -1L, "", str_sub(string, start = end + 1L)))
     }
    

    So we are effectively passing three arguments to the str_c function, one or more character vectors, an insertion string, and a collapse parameter (the ifelse bit). If the results of running just str_sub without using the assignment function are (if we already ran the first str_sub:

    > (test.string <- str_sub(x, start = 1L, end = -2 - 1L)) #start defaults to 1L
    [1] "A:B"   "E:FG"  "I:JKL"
    > replace.string <- ":"
    > (collapse.string <- ifelse(end == -1L, "", str_sub(string, start = end + 1L)))
    [1] "C"
    > str_c(test.string, replace.string, collapse.string)
    [1] "A:B:C"   "E:FG:C"  "I:JKL:C"
    

    So first we’re saving everything to the left of the symbol you want to replace, and then we’re setting a collapse parameter. The collapse parameter is kind of interesting, if you look at the docs for str_c, you’ll find that it says

    If collapse is … non-‘NULL’ that string is inserted at the end of each row, and the entire matrix collapsed to a single string.

    So that’s exactly what’s happening here, when we replace our strings, it is adding the collapse parameter to the end of each of our strings.

    But actually, this would work if the ifelse function wasn’t used, because without the ifelse, str_sub(string, start = end + 1L) would return [1] "C" "H" "M" instead of just taking the first index, “C”.

    So this is why when we add a start and end value of c(-2, -2, -2) instead we can get the right answer:

    > (test.string <- str_sub(x, start = 1L, end = c(-2, -2, -2) - 1L)) #start defaults to 1L
    [1] "A:B"   "E:FG"  "I:JKL"
    > replace.string <- ":"
    > (collapse.string <- ifelse(end == -1L, "", str_sub(string, start = c(-2, -2, -2) + 1L)))
    [1]  "C" "H" "M"
    > str_c(test.string, replace.string, collapse.string)
    [1] "A:B:C"   "E:FG:C"  "I:JKL:C"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to replace all the carriage return characters in a string obtained from
I'm trying to remove all (non-space) whitespace characters from a file and replace all
I'm trying to use preg_replace to remove all characters from a string except for
I am trying to replace certain patterns in a string with different replacement patters.
im trying to use a bash script to replace all none numeric characters from
I am trying to replace all $ characters in a String expression like this
I'm trying to replace in VIM all multiple - characters (from the start of
I am trying to replace in a string all non word characters with empty
My problem is simple. I am trying to replace all the <br/> characters from
I am trying to replace all German special characters in a Regular Expression. The

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.