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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:22:36+00:00 2026-06-12T18:22:36+00:00

As subset() manual states: Warning : This is a convenience function intended for use

  • 0

As subset() manual states:

Warning: This is a convenience function intended for use interactively

I learned from this great article not only the secret behind this warning, but a good understanding of substitute(), match.call(), eval(), quote(), ‍call, promise and other related R subjects, that are a little bit complicated.

Now I understand what’s the warning above for. A super-simple implementation of subset() could be as follows:

subset = function(x, condition) x[eval(substitute(condition), envir=x),]

While subset(mtcars, cyl==4) returns the table of rows in mtcars that satisfy cyl==4, enveloping subset() in another function fails:

sub = function(x, condition) subset(x, condition)

sub(mtcars, cyl == 4)
# Error in eval(expr, envir, enclos) : object 'cyl' not found

Using the original version of subset() also produces exactly the same error condition. This is due to the limitation of substitute()-eval() pair: It works fine while condition is cyl==4, but when the condition is passed through the enveloping function sub(), the condition argument of subset() will be no longer cyl==4, but the nested condition in the sub() body, and the eval() fails – it’s a bit complicated.

But does it exist any other implementation of subset() with exactly the same arguments that would be programming-safe – i.e. able to evaluate its condition while it’s called by another function?

  • 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-12T18:22:37+00:00Added an answer on June 12, 2026 at 6:22 pm

    Just because it’s such mind-bending fun (??), here is a slightly different solution that addresses a problem Hadley pointed to in comments to my accepted solution.

    Hadley posted a gist demonstrating a situation in which my accepted function goes awry. The twist in that example (copied below) is that a symbol passed to SUBSET() is defined in the body (rather than the arguments) of one of the calling functions; it thus gets captured by substitute() instead of the intended global variable. Confusing stuff, I know.

    f <- function() {
      cyl <- 4
      g()
    }
    
    g <- function() {
      SUBSET(mtcars, cyl == 4)$cyl
    }
    f()
    

    Here is a better function that will only substitute the values of symbols found in calling functions’ argument lists. It works in all of the situations that Hadley or I have so far proposed.

    SUBSET <- function(`_dat`, expr) {
       ff <- sys.frames()
       n <- length(ff)
       ex <- substitute(expr)
       ii <- seq_len(n)
       for(i in ii) {
           ## 'which' is the frame number, and 'n' is # of frames to go back.
           margs <- as.list(match.call(definition = sys.function(n - i),
                                       call = sys.call(sys.parent(i))))[-1]
           ex <- eval(substitute(substitute(x, env = ll),
                                 env = list(x = ex, ll = margs)))
       }
       `_dat`[eval(ex, envir = `_dat`),]
    }
    
    ## Works in Hadley's counterexample ...
    f()
    # [1] 4 4 4 4 4 4 4 4 4 4 4
    
    ## ... and in my original test cases.
    sub <- function(x, condition) SUBSET(x, condition)
    sub2 <- function(AA, BB) sub(AA, BB)
    
    a <- SUBSET(mtcars, cyl == 4)  ## Direct call to SUBSET()
    b <- sub(mtcars, cyl == 4)     ## SUBSET() called one level down
    c <- sub2(mtcars, cyl == 4)
    all(identical(a, b), identical(b, c))
    # [1] TRUE
    

    IMPORTANT: Please note that this still is not (nor can it be made into) a generally useful function. There’s simply no way for the function to know which symbols you want it to use in all of the substitutions it performs as it works up the call stack. There are many situations in which users would want it to use the values of symbols assigned to within function bodies, but this function will always ignore those.

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

Sidebar

Related Questions

Got this question from algorithms design manual by Steven Skiena. It is required to
Following from these question Subset sum problem and Sum-subset with a fixed subset size
I am trying to sample a subset from data with replacement and here I
i have this super manual: http://www.arib.or.jp/IMT-2000/V710Dec08/5_Appendix/R99/27/27005-320.pdf i dont understand whether it is for every
I am using a subset to extract from set of a column values I
Is this a subset of .NET or is it the complete framework? If it's
How can I subset a table based on values and return those values? This
Trying to get a subset of a data frame based on, to borrow from
I'm trying to retrieve only a subset of fields from mongodb using Java driver.
The following is a subset of xml from a twitter atom feed: <entry> <id>tag:search.twitter.com,2005:18232030105964545</id>

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.