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.
I don’t recommend it, butyou could do:lapply(myList, 'attr<-', which='myname', value='myStaticName'). An old fashionedforloop is probably the clearest way to perform this task—or do this assignment upstream when the objects are created.EDIT:
As @mnel points out in the comments,
setattrin thedata.tablepackage is also an efficient option, since it assigns by reference.Edit: @mnel — don’t use setattr with
lapply. This is one case where theforloop is much faster.