[R] how to iterate through a list using ls

Erik Iverson iverson at biostat.wisc.edu
Sun Nov 2 01:26:24 CET 2008


See ?get

However, it sounds like you might look at storing your data.frames in an 
object of class "list".  Then you can do things like:


alist <- list(df1 = data.frame(a = 1:10, b = 2:11), df2 = data.frame(a = 
2:100, b = 3:101))

lapply(alist, summary)

If you wrote your own function to do things to data.frames, you could 
use lapply with your own function.  It's a pretty powerful way of doing 
a lot with a few lines of code, and you don't have to worry about how 
many objects there are, what they are called, or using 'get', etc.


Lo, Ken wrote:
> Hi all,
> 
> I know this is an idiotic question, but I am trying to iterate through a
> list of data.frame using ls (I have named the data frames in a way that
> lets me subset them with ls(pattern="test", for example).  But ls
> returns a character vector.  How to I refer to the data frames by their
> names in the list instead of the actual string?
> 
> Thanks a bunch.
> 
> Ken
> 
> ______________________________________________
> 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