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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:07:43+00:00 2026-05-26T07:07:43+00:00

Problem I would like to test if an element of a list exists, here

  • 0

Problem

I would like to test if an element of a list exists, here is an example

foo <- list(a=1)
exists('foo') 
TRUE   #foo does exist
exists('foo$a') 
FALSE  #suggests that foo$a does not exist
foo$a
[1] 1  #but it does exist

In this example, I know that foo$a exists, but the test returns FALSE.

I looked in ?exists and have found that with(foo, exists('a') returns TRUE, but do not understand why exists('foo$a') returns FALSE.

Questions

  • Why does exists('foo$a') return FALSE?
  • Is use of with(...) the preferred approach?
  • 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-05-26T07:07:44+00:00Added an answer on May 26, 2026 at 7:07 am

    This is actually a bit trickier than you’d think. Since a list can actually (with some effort) contain NULL elements, it might not be enough to check is.null(foo$a). A more stringent test might be to check that the name is actually defined in the list:

    foo <- list(a=42, b=NULL)
    foo
    
    is.null(foo[["a"]]) # FALSE
    is.null(foo[["b"]]) # TRUE, but the element "exists"...
    is.null(foo[["c"]]) # TRUE
    
    "a" %in% names(foo) # TRUE
    "b" %in% names(foo) # TRUE
    "c" %in% names(foo) # FALSE
    

    …and foo[["a"]] is safer than foo$a, since the latter uses partial matching and thus might also match a longer name:

    x <- list(abc=4)
    x$a  # 4, since it partially matches abc
    x[["a"]] # NULL, no match
    

    [UPDATE] So, back to the question why exists('foo$a') doesn’t work. The exists function only checks if a variable exists in an environment, not if parts of a object exist. The string "foo$a" is interpreted literary: Is there a variable called “foo$a”? …and the answer is FALSE…

    foo <- list(a=42, b=NULL) # variable "foo" with element "a"
    "bar$a" <- 42   # A variable actually called "bar$a"...
    ls() # will include "foo" and "bar$a" 
    exists("foo$a") # FALSE 
    exists("bar$a") # TRUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Problem: I would like to share code between multiple assemblies. This shared code will
I have a reasonably complex layout problem: I would like to have a main
while designing my user control, i encountered the following problem: i would like to
I have a problem and i would like to learn the correct way to
I have a problem that I would like have solved via a SQL query.
I have a little problem where I would like to insert a svn diff
I have an interesting genetics problem that I would like to solve in native
I need to solve a job affectation problem and I would like to find
I would like solve the problem (now hypothetical but propably real in future) of
I would like your advice about how best to solve my problem. In a

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.