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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:35:46+00:00 2026-05-25T15:35:46+00:00

I am applying a function to the elements of a list. the list has

  • 0

I am applying a function to the elements of a list.

the list has names, so in a sense each element has its own name, but how do I access it once the lapply function has already extracted/separated the element from the list?

Some fictive data (as internal function I’m here misusing dput):

r <- list(a=structure(1:4, unit="test"), b='abc')
lapply(r, dput)

What I observe here is that dput receives the objects in the list as if accessed with [[, deprived of the name they have in the containing list.

So I thought I would drop the idea of using functions from the apply family and write a loop, but I don’t particularly like the idea and it obliges me to construct the result of the complete function.

result <- list()
for (name in names(r)) {
  print(name)
  result[[name]] <- dput(r[[name]])
}
result

Any insightful thoughts?

  • 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-25T15:35:47+00:00Added an answer on May 25, 2026 at 3:35 pm

    You can simulate the idea behind loops whilst still using lapply, by passing a numeric vector to lapply and then using that as an index to extract the elements from the list you want. That probably makes no sense, but hopefully the example illustrates what I mean:

    lapply(seq_along(r), function(i)dput(r[i]))
    
    structure(list(a = structure(1:4, unit = "test")), .Names = "a")
    structure(list(b = "abc"), .Names = "b")
    [[1]]
    [[1]]$a
    [1] 1 2 3 4
    attr(,"unit")
    [1] "test"
    
    
    
    [[2]]
    [[2]]$b
    [1] "abc"
    

    The key idea is that seq_along(x) returns a sequence of the same length as x. For example:

    > seq_along(r)
    [1] 1 2
    

    See ?seq_along for more detail.


    EDIT

    This seems to be very marginally faster than indexing by name:

    library(rbenchmark)
    benchmark(
      xx <- lapply(names(r), function(i)dput(r[i])),
      yy <- lapply(seq_along(r), function(i)dput(r[i])),
      replications=10000)
    
                                                    test replications elapsed relative user.self
    1     xx <- lapply(names(r), function(i) dput(r[i]))        10000    1.95 1.026316      1.70
    2 yy <- lapply(seq_along(r), function(i) dput(r[i]))        10000    1.90 1.000000      1.66
      sys.self user.child sys.child
    1     0.00         NA        NA
    2     0.01         NA        NA
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How would I go about applying each element in a list to each argument
I am applying an event listener function when elements are inserted into the document
What would you name a function that takes a list and a function, and
I have one list with points. I am applying: var result = neighbors.SelectMany(element =>
I'm applying the strikeout tag: <s>$5,000,000</s> But the line is too low.. .it's about
What's the most efficient way to lower case every element of a List or
[SOLVED]: Applying proper list iteration procedure fixed problem. (Shown below) I currently have a
Just want to remove highlight class before applying class again on other elements. Reseting
I am repeatedly applying a function to read and process a bunch of csv
I'm trying to generate a matrix by applying a function to all combinations of

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.