[R] How to write a list object's name to a new dataframe in that list object

R. Michael Weylandt <michael.weylandt@gmail.com> michael.weylandt at gmail.com
Sat Oct 8 05:42:16 CEST 2011


It's inefficient but try this:

Suppose your list is L:

mapply(function(x,y) cbind(x, data.frame(NAME=rep(y,NROW(x)))), L, names(L), simplify = F)

Code is untested however - let me know if this doesn't work and I'll get to a computer and clean it up. 

Michael Weylandt


On Oct 7, 2011, at 11:17 PM, Mark Na <mtb954 at gmail.com> wrote:

> Hello R-helpers,
> 
> I have a list that only contains dataframes. Each element of the list
> (i.e., each dataframe) has a unique name ("one" through "ten"). I wish
> to add a new column (called "NAME") to each list element (i.e each
> datarame) and I want that column to contain the name of it's list
> element.
> 
> e.g. the list element (i.e., dataframe) called "one" would get a new
> column called "NAME" that would contain the word "one" in every row.
> 
> Could anyone help with that?
> 
> Many thanks,
> 
> Mark Na
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list