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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:42:03+00:00 2026-05-27T19:42:03+00:00

I have text data (in R) and want to replace some characters with other

  • 0

I have text data (in R) and want to replace some characters with other characters in a data frame. I thought this would be an easy task using strsplit on spaces and create a vector that can I can then use matching (%in%) which can then be pasted back together. But then I thought about punctuation. There’s no space between the last word of a sentence and the punctuation at the end.

I figure there’s probably a more simple way to achieve what I want than the convoluted mess that’s becoming my code. I would appreciate direction with this problem.

#Character String
x <- "I like 346 ice cream cones.  They're 99 percent good!  I ate 46."

#Replacement Values Dataframe
  symbol text                     
1 "346"  "three hundred forty six"
2 "99"   "ninety nine"            
3 "46"   "forty six" 

#replacement dataframe
numDF <- 
data.frame(symbol = c("346","99", "46"),
           text = c("three hundred forty six", "ninety nine","forty six"),
           stringsAsFactors = FALSE)

Desired outcome:

[1] "I like three hundred forty six ice cream cones.  They're ninety nine percent good!  You ate forty six?")

EDIT: I originally entitled this conditional gsub because that what it seems like to me even though there is no gsub involved.

  • 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-27T19:42:04+00:00Added an answer on May 27, 2026 at 7:42 pm

    Maybe this, inspired by Josh O’Brien’s answer, does it:

    x <- "I like 346 ice cream cones.  They're 99 percent good!  I ate 46."
    numDF <- structure(c("346", "99", "46", "three hundred forty six", "ninety nine", 
    "forty six"), .Dim = c(3L, 2L), .Dimnames = list(c("1", "2", 
    "3"), c("symbol", "text")))
    
    pat <-  paste(numDF[,"symbol"], collapse="|")
    repeat {
        m <- regexpr(pat, x)
        if(m==-1) break
        sym <- regmatches(x,m)
        regmatches(x,m) <- numDF[match(sym, numDF[,"symbol"]), "text"]
    }
    x
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a lot of text data and want to translate it to different
I have a series of tables that contain data I want to full text
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have text stored in SQL as HTML. I'm not guaranteed that this data
I have a .rc file which is used to include some text data in
If i have data in my MySQL like this table: data(TEXT) foo (hal) foo
I have, for example, markup like this <div id=content> <p>Here is some wonderful text,
In my NText column, I have some data in between <script type='text/javascript'> </script> tags.
I have rows of text data that can vary between 0 and 100, and
I have Text file that contains data separated with a comma , . How

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.