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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:47:01+00:00 2026-06-02T17:47:01+00:00

How to remove all special characters from string in R and replace them with

  • 0

How to remove all special characters from string in R and replace them with spaces ?

Some special characters to remove are : ~!@#$%^&*(){}_+:"<>?,./;'[]-=

I’ve tried regex with [:punct:] pattern but it removes only punctuation marks.

Question 2 : And how to remove characters from foreign languages like : â í ü Â á ą ę ś ć ?

Answer : Use [^[:alnum:]] to remove~!@#$%^&*(){}_+:"<>?,./;'[]-= and use [^a-zA-Z0-9] to remove also â í ü Â á ą ę ś ć in regex or regexpr functions.

Solution in base R :

x <- "a1~!@#$%^&*(){}_+:\"<>?,./;'[]-=" 
gsub("[[:punct:]]", "", x)  # no libraries needed
  • 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-02T17:47:02+00:00Added an answer on June 2, 2026 at 5:47 pm

    You need to use regular expressions to identify the unwanted characters. For the most easily readable code, you want the str_replace_all from the stringr package, though gsub from base R works just as well.

    The exact regular expression depends upon what you are trying to do. You could just remove those specific characters that you gave in the question, but it’s much easier to remove all punctuation characters.

    x <- "a1~!@#$%^&*(){}_+:\"<>?,./;'[]-=" #or whatever
    str_replace_all(x, "[[:punct:]]", " ")
    

    (The base R equivalent is gsub("[[:punct:]]", " ", x).)

    An alternative is to swap out all non-alphanumeric characters.

    str_replace_all(x, "[^[:alnum:]]", " ")
    

    Note that the definition of what constitutes a letter or a number or a punctuatution mark varies slightly depending upon your locale, so you may need to experiment a little to get exactly what you want.

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

Sidebar

Related Questions

I need to remove all special characters, punctuation and spaces from a string so
I want to remove all special characters from a string. Allowed characters are A-Z
I want to remove all special characters except space from a string using JavaScript.
How can I remove from a string all characters except white spaces, numbers, and
I would like a RegExp that will remove all special characters from a string.
We need a C# function which will remove all special characters from a string.
What is the best way to remove all the special characters from a string
I want to remove all special characters from a string. Allowed characters are A-Z
If I remove all the ' characters from a SQL query, is there some
I would like to trim all special characters from a string in SQL. I've

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.