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

  • SEARCH
  • Home
  • 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 8441363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:32:50+00:00 2026-06-10T08:32:50+00:00

How can I (fastest preferable) remove commas from a digit part of a string

  • 0

How can I (fastest preferable) remove commas from a digit part of a string without affecting the rest of the commas in the string. So in the example below I want to remove the comas from the number portions but the comma after dog should remain (yes I know the comma in 1023455 is wrong but just throwing a corner case out there).

What I have:

x <- "I want to see 102,345,5 dogs, but not too soo; it's 3,242 minutes away"

Desired outcome:

[1] "I want to see 1023455 dogs, but not too soo; it's 3242 minutes away"

Stipulation: must be done in base no add on packages.

Thank you in advance.

EDIT:
Thank you Dason, Greg and Dirk. Both your responses worked very well. I was playing with something close to Dason’s response but had the comma inside the parenthesis. Now looking at it that doesn’t even make sense. I microbenchmarked both responses as I need speed here (text data):

Unit: microseconds
         expr     min      lq  median      uq     max
1  Dason_0to9  14.461  15.395  15.861  16.328  25.191
2 Dason_digit  21.926  23.791  24.258  24.725  65.777
3        Dirk 127.354 128.287 128.754 129.686 154.410
4      Greg_1  18.193  19.126  19.127  19.594  27.990
5      Greg_2 125.021 125.954 126.421 127.353 185.666

+1 to all of you.

  • 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-10T08:32:51+00:00Added an answer on June 10, 2026 at 8:32 am

    You could replace anything with the pattern (comma followed by a number) with the number itself.

    x <- "I want to see 102,345,5 dogs, but not too soo; it's 3,242 minutes away"
    gsub(",([[:digit:]])", "\\1", x)
    #[1] "I want to see 1023455 dogs, but not too soo; it's 3242 minutes away"
    #or
    gsub(",([0-9])", "\\1", x)
    #[1] "I want to see 1023455 dogs, but not too soo; it's 3242 minutes away"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

what's the fastest way to get only the important_stuff part from a string like
What's the fastest method , to remove a specific extension from a String by
What is the fastest timing system a C/C++ programmer can use? For example: time()
Can someone explain which indexed search would be faster, listing from fastest, to slowest
can someone tell me the fastest way to parse a json string to an
How can I test what is fastest? I want to test which of the
How can i create an associative array using the array below, In the fastest
How can I load a web-page into a string in .net ? i want
How the fastest can I transfer the data block of 256 bytes from one
I'm wondering what is the fastest way that I can write some code. I

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.