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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:46:43+00:00 2026-05-31T12:46:43+00:00

A post on here a day back has me wondering how to assign values

  • 0

A post on here a day back has me wondering how to assign values to multiple objects in the global environment from within a function. This is my attempt using lapply (assign may be safer than <<- but I have never actually used it and am not familiar with it).

#fake data set
df <- data.frame(
  x.2=rnorm(25),
  y.2=rnorm(25),
  g=rep(factor(LETTERS[1:5]), 5)
)

#split it into a list of data frames
LIST <- split(df, df$g)

#pre-allot 5 objects in R with class data.frame()
V <- W <- X <- Y <- Z <- data.frame()

#attempt to assign the data frames in the LIST to the objects just created
lapply(seq_along(LIST), function(x) c(V, W, X, Y, Z)[x] <<- LIST[[x]])

Please feel free to shorten any/all parts of my code to make this work (or work better/faster).

  • 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-31T12:46:44+00:00Added an answer on May 31, 2026 at 12:46 pm

    Update of 2018-10-10:

    The most succinct way to carry out this specific task is to use list2env() like so:

    ## Create an example list of five data.frames
    df <- data.frame(x = rnorm(25),
                     g = rep(factor(LETTERS[1:5]), 5))
    LIST <- split(df, df$g)
    
    ## Assign them to the global environment
    list2env(LIST, envir = .GlobalEnv)
    
    ## Check that it worked
    ls()
    ## [1] "A"    "B"    "C"    "D"    "df"   "E"    "LIST"
    

    Original answer, demonstrating use of assign()

    You’re right that assign() is the right tool for the job. Its envir argument gives you precise control over where assignment takes place — control that is not available with either <- or <<-.

    So, for example, to assign the value of X to an object named NAME in the the global environment, you would do:

    assign("NAME", X, envir = .GlobalEnv)
    

    In your case:

    df <- data.frame(x = rnorm(25),
                     g = rep(factor(LETTERS[1:5]), 5))
    LIST <- split(df, df$g)
    NAMES <- c("V", "W", "X", "Y", "Z")
    
    lapply(seq_along(LIST), 
           function(x) {
               assign(NAMES[x], LIST[[x]], envir=.GlobalEnv)
            }
    )
    
    ls()
    [1] "df"    "LIST"  "NAMES" "V"     "W"     "X"     "Y"     "Z"    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first post here as I'm stuck with my wonderful C++ function. The error I'm
first post here, and probably an easy one. I've got the code from Processing's
this has been driving me crazy all day so I figured I'd post it
First post here... I normally develop using PHP and Symfony with Propel and ActionScript
first post here, I come in peace :) I've searched but can't quite find
My first post here :) I have a summer job doing a bit of
first time post here. I was hoping someone could help me make custom SPARQL
first thread i post here. I have been searching here for answers and almost
There is a post here that talks about this as being a bad idea
This is my first post here so go easy. I am trying to build

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.