[R] Using processed objects as arguments of a function

David Winsemius dwinsemius at comcast.net
Mon Nov 2 15:44:51 CET 2009


This would return a vector the names of objects whose names begin with  
"fund":

ls()[grep("^fund", ls())]

I'm such a grep-noob that I don't know off the top of my head what the  
pattern should be to restrict it to only those objects with digits in  
the next position.

Perhaps:
do.call( "rbind", sapply(ls()[grep("^m", ls())], get) )

-- 
David.

On Nov 2, 2009, at 5:24 AM, Jim Lemon wrote:

> On 11/02/2009 05:04 PM, Steven Kang wrote:
>> Dear R users,
>>
>> I wish to utilise processed and saved objects as arguments of a  
>> function.
>>
>> Specifically, I have created objects using *"assign"*&  *"paste"*  
>> functions
>> with an incremental index i, the names of the objects are:
>>
>>     fund1, fund2, fund3,....., fund80,..... (where the numerical  
>> value
>> increments according to the index i&  class of these objects are  
>> dataframes)
>>
>> I wish to collapse these objects row wisely using *"rbind"* function.
>>
>> paste("fund", 1:i, sep = "") results in list of objects as  
>> characters...&
>> get(paste("fund", 1:i, sep = "")) outputs fund1...
>>
>> Are there any methods to use these objects as an argument of  
>> "rbind" to
>> collapse the dataframes?
>>
>> Your expertise in resolving this issue would be highly appreciated.
> Hi Steven,
> There is probably a neater way to construct the list of dataframes,  
> but this will probably do what you want:
>
> dnames<-paste("fund",1:nfunds,sep="")
> makelist<-function(x) {
> nitems<-length(x)
> newlist<-vector("list",nitems)
> for(item in 1:nitems) newlist[[item]]<-get(x[item])
> return(newlist)
> }
> dflist<-makelist(dfnames)
> do.call("rbind",dflist)
>
> Of course all of the dataframes must have the same number of columns  
> or the result will be messy or not there at all.
>
> Jim
>
> ______________________________________________
> 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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list