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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:22:57+00:00 2026-06-17T09:22:57+00:00

I lave a non-nested list (pretty straight forward). Some elements are NAs but can

  • 0

I lave a non-nested list (pretty straight forward). Some elements are NAs but can be replaced with other elements from the same list. I can achieve this with the global assignment operator <<-. However I’m trying to learn better practice (As this appears to give me notes when compiling a package for CRAN). Questions:

  1. Can I achieve this without a global assignment?
  2. If not how can I use assign appropriately (my approach seems to make a bunch of copies of the same data set and may run into memory issues).

I have tried assign and it doesn’t work. I also tried to use lapply without global assignment but I can only get the last element returned rather than the list with each element replaced.

Here’s the problem:

#Fake Data
L1 <- lapply(1:3, function(i) rnorm(1))
L1[4:5] <- NA
names(L1) <- letters[1:5]

#items to replace and their replacements (names of elements to replace)
nulls <- names(L1[sapply(L1, function(x) is.na(x))])
replaces <- c("b", "a")

#doesn't work (returns only last element)
lapply(seq_along(nulls), function(i) {
    L1[[nulls[i]]] <- L1[[replaces[i]]]
    return(L1)
})

#works but considered bad practice by many
lapply(seq_along(nulls), function(i) {
    L1[[nulls[i]]] <<- L1[[replaces[i]]]
})

#does not work (I try L1[["d"]] and still get NA)
lapply(seq_along(nulls), function(i) {
    assign(paste0("L1[[", nulls[i], "]]"), L1[[replaces[i]]], envir = .GlobalEnv)
})

#also bad practice bu I tried
lapply(seq_along(nulls), function(i) {
    assign(paste0("L1$", nulls[i]), L1[[replaces[i]]], envir = .GlobalEnv)
})

#This works but it feels like we're making a ton of copies of the same data set
lapply(seq_along(nulls), function(i) {
    L1[[nulls[i]]] <- L1[[replaces[i]]]
    assign("L1", L1, envir = .GlobalEnv)
})  

Ultimately, I’d like to do this without global assignment but if not that what is best practice for a for CRAN build of a package.

  • 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-17T09:22:57+00:00Added an answer on June 17, 2026 at 9:22 am

    There’s a replace function that will do this for you:

    replace(L1, match(nulls, names(L1)), L1[match(replaces, names(L1))])
    

    You could also use the slightly simpler which(is.na(L1)), instead of match(nulls, names(L1))

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

Sidebar

Related Questions

I have to encode an existing php page to edit it. Can you help
Update: I am sorry i pasted in list.xml twice by mistake, my question is
Why is this giving me a lock timeout: for(int i = 0; i <
When you create a form element with CakePHP like so: echo $this->Form->input('User.first_name'); You start

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.