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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:50:16+00:00 2026-06-15T03:50:16+00:00

Or put it more general: How can I add multiple attributes to the elements

  • 0

Or put it more general: How can I add multiple attributes to the elements of list?

I am stuck trying to set an attribute to elements of a list all of which are data.frames. In the end I would like to add names(myList) as a varying attribute to every data.frame inside. But I even cannot get a static attribute for all list elements to go.

lapply(myList,attr,which="myname") <- "myStaticName"

This does not work because lapply does not work with lapply<-. If I had at least an idea how to do this, maybe I could figure out how to do it with varying attributes like the name of the list.

  • 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-15T03:50:18+00:00Added an answer on June 15, 2026 at 3:50 am

    I don’t recommend it, but you could do: lapply(myList, 'attr<-', which='myname', value='myStaticName'). An old fashioned for loop is probably the clearest way to perform this task—or do this assignment upstream when the objects are created.

    for (i in seq_along(myList)) attr(myList[[i]], 'myname') <- 'myStaticName'
    

    EDIT:

    As @mnel points out in the comments, setattr in the data.table package is also an efficient option, since it assigns by reference.

    Edit: @mnel — don’t use setattr with lapply. This is one case where the for loop is much faster.

    library(microbenchmark)
    library(data.table)
    myList <- as.list(1:10000)
    
    `lapply.attr<-` <- 
        function() 
        lapply(myList, 'attr<-', which='myname', value='myStaticName')
    
    `for.attr<-` <- 
        function() 
        for (i in seq_along(myList)) 
            attr(myList[[i]], 'myname') <- 'myStaticName'
    
    lapply.setattr <- 
      function() 
      lapply(myList, setattr, name='myname', value='myStaticName')
    
    for.setattr <- function() 
      for (i in seq_along(myList)) 
      setattr(myList[[i]], name = 'myname', value = 'myStaticName')
    
    result <- microbenchmark(`lapply.attr<-`(), `for.attr<-`(), lapply.setattr(), for.setattr())
    plot(result)
    

    enter image description here

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

Sidebar

Related Questions

I cannot put jChceckBox to jTable cell. More likely I can put checkBox to
Put them all in one separate folder structure or along with classes which implements
Say TABLE-A can have one or more rows in TABLE-B, which can have one
I was put in a position today in which I needed to enumerate all
As I'm starting to put more tracing in my code, i'm realizing it adds
Sometimes I feel the urge to put some more expressiveness in my git commit
The more detail I put in an interface, the less reusable it is. On
How best to put the array (100 or more length) in the database (MySQL)?
To make this more clear, I'm going to put code samples: $file = fopen('filename.ext',
Working on parsing a bunch of databases put together in an older, more freewheeling

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.