SImpler:

for(nm in names(L) ) L[[nm]] [[nm]] <- nm

However, note that you could also attach the name to each data frame as a,
say, "name" attribute and avoid the extra column.

-- Bert

On Fri, Oct 7, 2011 at 8:42 PM, R. Michael Weylandt <
michael.weylandt@gmail.com> <michael.weylandt@gmail.com> wrote:

> 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@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@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.
>
> ______________________________________________
> R-help@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.
>



-- 
"Men by nature long to get on to the ultimate truths, and will often be
impatient with elementary studies or fight shy of them. If it were possible
to reach the ultimate truths without the elementary studies usually prefixed
to them, these would not be preparatory studies but superfluous diversions."

-- Maimonides (1135-1204)

Bert Gunter
Genentech Nonclinical Biostatistics

	[[alternative HTML version deleted]]

