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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T09:29:18+00:00 2026-05-31T09:29:18+00:00

I have two pieces of code I use often in which I use the

  • 0

I have two pieces of code I use often in which I use the <<- to assign to the Global environment from within a function. I know I should be using assign as it gives better control and is more predictable. I am trying to wrap my head around using assign but can’t transfer the <<- code to code that uses assign:

A FAKE DATA SET AND THE TWO PIECES OF CODE WITH THE <<-

#CREATE A FAKE DATA SET
df <- data.frame(
    x.2=rnorm(25),
    y.2=rnorm(25),
    g=rep(factor(LETTERS[1:5]), 5)
)
#Use split to make a list of data frames
LIST <- split(df, df$g) #split it into a list of data frames
NAMES <- names(LIST) #save the names of this for later use 
LIST <- lapply(seq_along(LIST), function(x) as.data.frame(LIST[[x]])[, 1:2])

#THE TWO PIECES OF CODE THAT USE <<-
#Use Global Assignment to Change All Variable Names of Data Frames in a List
lapply(seq_along(LIST), function(x) names(LIST[[x]]) <<-
    unlist(strsplit(names(LIST[[x]])[1:length(names(LIST[[x]]))],
    ".", fixed=T))[c(T, F)]
)
LIST

#Rename All the Data Frames in the List Using Global Assignment
lapply(seq_along(LIST), function(x) names(LIST)[[x]] <<- NAMES[x])
LIST

My attempts to use assign:

lapply(seq_along(LIST), function(x) {
    assign(names(LIST[[x]]), 
    unlist(strsplit(names(LIST[[x]])[1:length(names(LIST[[x]]))],
    ".", fixed=T))[c(T, F)],  envir=.GlobalEnv)
    }
)
LIST

lapply(seq_along(LIST), function(x) assign(names(LIST)[[x]], 
    NAMES[x], envir=.GlobalEnv))
LIST

Please help me to do this correctly and axplain what is wrong with my approach. Thank you in advance.

  • 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-31T09:29:19+00:00Added an answer on May 31, 2026 at 9:29 am

    I think this is the same thing

    LIST <- lapply(seq_along(LIST), function(x) {
      names(LIST[[x]]) <- 
        unlist(strsplit(names(LIST[[x]])[1:length(names(LIST[[x]]))],
                        ".", fixed=T))[c(T, F)]
      LIST[[x]]
    })
    LIST
    
    names(LIST) <- NAMES
    LIST
    

    or, to use assign

    assign("LIST", lapply(seq_along(LIST), function(x) {
      names(LIST[[x]]) <- 
        unlist(strsplit(names(LIST[[x]])[1:length(names(LIST[[x]]))],
                        ".", fixed=T))[c(T, F)]
      LIST[[x]]
    }), pos=.GlobalEnv)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written two pieces of code which I intended to have identical outputs,
I have two pieces of code that I need to use together. I am
I have two pieces of C++ code running on 2 different cores. Both of
I have two OptionMenu widgets in the simple pieces of code shown below: variable
I have two classes (MVC view model) which inherits from one abstract base class.
I have following two pieces of code First one is not explicitly disposing Image
In two different views I have the following two pieces of code. FIRST <table
I have two pieces of code that I'm using to learn about multiprocessing in
I have two duplicate pieces of code that I want to work independently. I
I have two pieces of code.They work properly when it is used alone in

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.