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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:47:56+00:00 2026-06-15T11:47:56+00:00

Hi I’m working with a large data frame that I frequently need to subset

  • 0

Hi I’m working with a large data frame that I frequently need to subset in different combinations of variables. I’d like to be able to store the search in a string so I can just refer to the string when I want to see a subset.

x = read.table(textConnection("
                              cat1 cat2 value
                              A     Z    1
                              A     Y    2
                              A     X    3
                              B     N    2"),header=T,strip.white=T)
search_string="cat1== 'A' & cat2=='Z'"
with(x,subset(x,search))

doesn’t work. What I’d be looking for is the result of a search similar to the one below.

with(x,subset(x,cat1=='A' & cat2=='Z'))

I’d prefer not to just create multiple subsetted data frames at the start if another solution exists.

Is there a simple way to do what I’m trying?

  • 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-15T11:47:57+00:00Added an answer on June 15, 2026 at 11:47 am

    You need to convert your string to an expression, and then evaluate this expression

     subset(x, eval(parse(text=search_string)))
    
      cat1 cat2 value
    1    A    Z     1
    

    Always remembering

    fortune(106)
    
    If the answer is parse() you should usually rethink the question.
       -- Thomas Lumley
          R-help (February 2005)
    
    fortune(181)
    
    Personally I have never regretted trying not to underestimate my own future stupidity.
       -- Greg Snow (explaining why eval(parse(...)) is often suboptimal, answering a question triggered by the
          infamous fortune(106))
          R-help (January 2007)
    

    And noting in this case, it is far more typing to go the eval(parse(text==)))

    You could also try using quote to save the call

    search <- quote(cat1== 'A' & cat2=='Z')
    

    and then just use

     subset(x, eval(search))
    

    It is also important to remember that that subset has nonstandard evaluation so it might be safer to use `[`

    x[eval(search,x),]
    

    or

     x[eval(parse(text=search_string)), x),]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into

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.