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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:26:23+00:00 2026-06-01T22:26:23+00:00

Must be a way to avoid doing this in R. Some kind of mapping

  • 0

Must be a way to avoid doing this in R. Some kind of mapping function? Basically, want to specify a series of grepl conditions and a series of replacements. Thanks,

hold[grepl('TRAVEL', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "TRAVEL"
hold[grepl('IN-KIND:', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "IN-KIND"
hold[grepl('COMPLIANCE', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "COMPLIANCE"
hold[grepl('MEDIA', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "MEDIA"
hold[grepl('WATER|FOOD|OFFICE|Office|CLEANING|ALARM', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "OFFICE"
hold[grepl('DATA', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "DATA"
hold[grepl('LEGAL', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "LEGAL"
hold[grepl('EVENT', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "EVENT"
hold[grepl('CATERING', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "CATERING"
hold[grepl('PHONE', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "PHONE"
hold[grepl('EQUIPMENT', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "EQUIPMENT"
hold[grepl('FUNDRAISING|FINANCE', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "FUNDRAISING"
hold[grepl('PAYROLL', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "PAYROLL"
hold[grepl('LIST RENTAL', hold[, "exp_pur_desc"]), "exp_pur_desc"] <- "LIST RENTAL"

Edit: Yeah, sorry, little flippant. hold is data.frame with one column named “exp_pur_desc.” I can see lapply or sapply working with a “switch” equivalent. Thanks.

  • 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-01T22:26:25+00:00Added an answer on June 1, 2026 at 10:26 pm

    Here’s one way:

    strReplace <- function(x, mapFrom, mapTo) {
        for (i in seq_along(mapFrom)) {
            x <- gsub(mapFrom[[i]], mapTo[[i]], x)
        }
        x
    }
    
    strReplace(c("Hello cruel world!", "Hello again Tellus!"), # Text
               c("Hello", "world|Tellus"), # words to replace (regular expressions)
               c("Hi", "moon"))            # words to replace with
    #[1] "Hi cruel moon!" "Hi again moon!"
    

    … so in your case:

    hold<-data.frame(ID=11:12, exp_pur_desc=c('FOOD','FINANCE'))
    hold[['exp_pur_desc']] <- strReplace(hold[['exp_pur_desc']],
      c('WATER|FOOD|OFFICE|Office|CLEANING|ALARM', 'FUNDRAISING|FINANCE'),
      c('OFFICE', 'FUNDRAISING'))
    hold
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: There must be some way I can approach this without writing a whole
There must be an easier way or function to do this code here: #!/usr/bin/env
Must be a way looping through this code: private void loadSprites() { this.sprites[0] =
I am sure there must be a relatively straightforward way to do this, but
I must be understanding the warnings documentation wrong. The way I read it, this
What's the shortest way to do this in Python? string = xyz must return
I'm a beginner in Flex so there must be more elegant way of doing
I'm pretty sure there must be a way to launch spotify iphone app from
Is there any way to disable localization in an Android application? It must use
I'm trying to find a way to to do something that I think must

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.