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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:10:05+00:00 2026-06-17T23:10:05+00:00

I’m looking to do something seemingly very simple. I would like to subset a

  • 0

I’m looking to do something seemingly very simple. I would like to subset a data frame in R using the grepl() command — or something like it — on several different phrases without constructing a loop.

For example, I’d like to pull out all the rows for anyone named Bob or Mary:

## example data frame:
tmp = structure(list(Name = structure(c(6L, 8L, 9L, 7L, 2L, 3L, 10L, 
1L, 5L, 4L), .Label = c("Alan", "Bob", "bob smith", "Frank", 
"John", "Mary Anne", "mary jane", "Mary Smith", "Potter, Mary", 
"smith, BOB"), class = "factor"), Age = c(31L, 23L, 23L, 55L, 
32L, 36L, 45L, 12L, 43L, 46L), Height = 1:10), .Names = c("Name", 
"Age", "Height"), class = "data.frame", row.names = c(NA, -10L
))

tmp

#           Name Age Height
#1     Mary Anne  31      1
#2    Mary Smith  23      2
#3  Potter, Mary  23      3
#4     mary jane  55      4
#5           Bob  32      5
#6     bob smith  36      6
#7    smith, BOB  45      7
#8          Alan  12      8
#9          John  43      9
#10        Frank  46     10

## this doesn't work
mynames=c('bob','mary')
tmp[grepl(mynames,tmp$Name,ignore.case=T),]

Any ideas would be helpful!

  • 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-17T23:10:06+00:00Added an answer on June 17, 2026 at 11:10 pm

    You can combine your mynames vector with the regular expression operator | and use grep.

    tmp[grep(paste(mynames, collapse='|'), tmp$Name, ignore.case=TRUE),]
    
    #           Name Age Height
    # 1    Mary Anne  31      1
    # 2   Mary Smith  23      2
    # 3 Potter, Mary  23      3
    # 4    mary jane  55      4
    # 5          Bob  32      5
    # 6    bob smith  36      6
    # 7   smith, BOB  45      7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
I want to construct a data frame in an Rcpp function, but when I
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I'm making a simple page using Google Maps API 3. My first. One marker
Seemingly simple, but I cannot find anything relevant on the web. What is the
I would like to count the length of a string with PHP. The string
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am using jsonparser to parse data and images obtained from json response. When
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) 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.