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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:46:27+00:00 2026-06-14T08:46:27+00:00

I have data frame that has a column with large number of file names

  • 0

I have data frame that has a column with large number of file names like:

d <- c("harry11_scott80_norm.avi","harry11_norm.avi","harry11_scott80_lpf.avi", 
       "joel51_lpf.avi","rich82_joel51_lpf.avi")

I want R to replace all expressions with two people names like harry11_scott80_norm.avi with the expression incongruent and all the ones with single person name like harry11_norm.avi with congruent. I could use gsub to do that:

dd <- gsub("harry11_scott80_norm.avi", "incongruent", d) 

but I got a lot of those names, so it would be a very clunky solution. So ideally I want to replace the ENTIRE expression that contains strings like _scott80_ with “incongruent”. I thought that gsub can do this, but when I run it:

dd <- gsub("_scott80_", "incongruent", d)

it returns with harry11incongruentnorm.avi, which is obviously because it simply replace the exact string match. I recon there is some way to tell gsub to replace expression entirely that contains selected string, but I can’t find it.

There was a question In R, how do I replace a string that contains a certain pattern with another string?, but I am not sure how to use agrep in this context.


EDIT: Side bonus question – based on @GSee answer, is there any function that allows you to pass a list of strings that you want to replace? For example, gsub(c(".*_scott80_.*", ".*_harry11_.*"), "incongruent", d) won’t work.

  • 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-14T08:46:28+00:00Added an answer on June 14, 2026 at 8:46 am

    Here’s one way

    > gsub(".*_scott80_.*", "incongruent", d)
    [1] "incongruent"           "harry11_norm.avi"      "incongruent"          
    [4] "joel51_lpf.avi"        "rich82_joel51_lpf.avi"
    

    Or with grep

    > d[grep("_scott80_", d)] <- "incongruent"
    > d
    [1] "incongruent"           "harry11_norm.avi"      "incongruent"          
    [4] "joel51_lpf.avi"        "rich82_joel51_lpf.avi"
    

    To address your edit, I believe this will do it (using | to mean “or”)

    gsub(".*(_scott80_|_harry11_).*", "incongruent", d)
    

    Of course, you don’t have any strings in d that match "_harry11_"

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

Sidebar

Related Questions

I have a column of data in a R data frame that has values
Suppose I have a data frame in R that has names of students in
I have a data frame with a numeric column called store that has some
I have a data.frame, d1, that has 7 columns, the 5th through 7th column
I have a data frame that has a format like the following: Month Frequency
I have a large data frame that has three identifiers. For example: df <-
I have a data frame in R that has come about from running some
I have a data.frame that looks like this: > head(ff.df) .id pio caremgmt prev
I have a data.frame in R that looks like this: score rms template aln_id
I have a data frame in R that looks like this: > TimeOffset, Source,

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.