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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T23:09:33+00:00 2026-06-04T23:09:33+00:00

I came across a confusing feature of subset function (using column name as a

  • 0

I came across a confusing “feature” of subset function (using column name as a vector name for subsetting does not work):

data(iris)
Species <- unique(iris$Species)
i <- 2
Species[i]
subset(iris, subset = Species == Species[i])

sp <- unique(iris$Species)
sp[i]
subset(iris, subset = Species == sp[i])

Could someone explain me, what happens here and why?

  • 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-04T23:09:35+00:00Added an answer on June 4, 2026 at 11:09 pm

    subset() will first look inside the dataframe for any object you mention, so in your first example Species[i] returns ‘setosa’ (the same as iris$Species[i]). Only when the object you specify cannot be found inside the data frame, R looks in the parent frames and will find the correct object there.

    So it all does work, you just don’t understand how it works. You could have read this in the help files :

    Note that subset will be evaluated in the data frame, so columns can
    be referred to (by name) as variables in the expression (see the
    examples).

    How does this come about?

    The reason is the following lines of code in subset() :

    e <- substitute(subset)
    r <- eval(e, x, parent.frame())
    
    • subset (or e) is in your example Species == Species[i]
    • x is in your example iris
    • parent.frame() returns in your example the global environment.

    The second argument of the call to eval, x is called envir. It is the environment (or list or data frame, …) where the expression is evaluated. In your case, R evaluates Species == Species[i] inside x, which is your data frame.

    The third argument, parent.frame(), is the enclosure. This is the environments that encloses the data frame you specified als environment, and is the place where R will look in case the variables aren’t found in the dataframe.

    See also ?eval

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

Sidebar

Related Questions

I came across a confusing fact during coding a script. How can i find
Came across a problem whereby I wanted the last time data was imported to
While reading up on POSIX threading, I came across an example of thread-specific-data. I
While using the BoneCP connection pooling, I came across the following confusions and would
During my work with expression trees for a few days now, I came across
Came across a problem which google searches could not help me with. I have
I came across an instance the other day where I had a function taking
I was reading through the code and came across this syntax: $customerIDs = array_map(function(Customer
Came across something like this today, and was wondering if there was an equivalent
I came across the following expression in someone else's code. I think it's terrible

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.