Mark Na
2011-Oct-08 03:17 UTC
[R] How to write a list object's name to a new dataframe in that list object
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. Michael Weylandt <michael.weylandt@gmail.com>
2011-Oct-08 03:42 UTC
[R] How to write a list object's name to a new dataframe in that list object
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.
Reasonably Related Threads
- Retain only those records from a dataframe that exist in another dataframe
- How to apply five lines of code to ten dataframes?
- Averaging dataframes that are stored in a list
- Adding header lines to a dataframe that is exported using write.csv
- How to translate a dataframe into the R code that makes that dataframe?