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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:06:01+00:00 2026-05-23T09:06:01+00:00

i have set some environment variables: envir1 <- new.env() assign(a, 7, envir=envir1) assign(b, 8,

  • 0

i have set some environment variables:

envir1 <- new.env()
assign("a", 7, envir=envir1)
assign("b", 8, envir=envir1)
assign("x", 9, envir=envir1)

now i have a list with some of this varibles and some numerics:

lis <- list(1,2, as.name("a"), 5, as.name("x"))

how can i get the numeric value in both ways, when it’s a variable name and when it’s a number

getNumbers2 <- function(li,pos) {
    ## Part where i dont know  
    return(li[[pos]]) ## dont works. the name of the variable is returned
}

getNumbers <- function(li,pos, env1) {
    environment(getNumbers2) <- env1
    getNumbers2(li,pos)
}

getNumbers(lis, 3, envir1)
a ## << wanna have 7 here
  • 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-23T09:06:01+00:00Added an answer on May 23, 2026 at 9:06 am

    Why are you storing the "a" and "x" as an R symbol?:

    R> str(l[[3]])
     symbol a
    

    If you’d just done:

    env1 <- new.env()
    assign("a", 7, envir=env1)
    assign("b", 8, envir=env1)
    assign("x", 9, envir=env1)
    ll <- list(1, 2, "a", 5, "x")
    

    Then this

    with(env1, get(ll[[3]]))
    

    would have worked:

    R> with(env1, get(ll[[3]]))
    [1] 7
    

    but that doesn’t work easily inside a function, because of scoping problems.

    However, seeing as with(env1, get(ll[[3]])) works, you probably don’t need the function. Also note that get() takes an environment as an argument:

    R> get(ll[[3]], envir = env1)
    [1] 7
    

    So we could write the function as

    getNumbers <- function(obj, pos, env) {
        get(obj[[pos]], envir = env)
    }
    

    Which does work:

    R> getNumbers(ll, 3, env1)
    [1] 7
    

    Edit: Just to be clear, if all that is required is to extract an object by name (the position bit threw me as that doesn’t make any sense), then, as @hadley mentions in a comment below, all we really need is to subset the environment using [[:

    R> env1[[ll[[3]]]]
    [1] 7
    

    which, I suppose, if you want to wrap it in a functions would be

    getNumbers <- function(obj, pos, env) {
        env[[obj[[pos]]]]
    }
    
    R> getNumbers(ll, 3, env1)
    [1] 7
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my init I have set some variables and one animate that uses those
Is it possible in some way to have dynamic environment variables in Linux? I
I'm working on some functions to set the environment variables for Visual Studio within
I have one AlertDialog which is working fine.I have set some background images to
I have to set some routing rules in my php application, and they should
I have java script code to set some of the properties of ajax controls.
I have a floated span with some text. I have set the overflow to
I'm using Google Test Framework to set some unit tests. I have got three
I have 16 bits. In each bit I can set some property and send
Do you have some predefined set of targets which all build.xml files you create

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.