[R] writing function:loop and rbind

arnaud Gaboury arnaud.gaboury at gmail.com
Mon May 24 17:23:42 CEST 2010


Maybe is there a neater solution, but the function mget() does the trick. So until further advice, I will work with your solution.

Thank you




> -----Original Message-----
> From: Joshua Wiley [mailto:jwiley.psych at gmail.com]
> Sent: Monday, May 24, 2010 5:07 PM
> To: arnaud Gaboury
> Cc: r-help at r-project.org
> Subject: Re: [R] writing function:loop and rbind
> 
> On Mon, May 24, 2010 at 7:00 AM, arnaud Gaboury
> <arnaud.gaboury at gmail.com> wrote:
> > One solution is to create a list, then do.call.
> > Here is my environment:
> >
> >> ls()
> >  [1] "DailyPL100416" "DailyPL100419" "DailyPL100420" "l"
> "ll"
> > "PLglobal"      "Pos100415"     "Pos100416"     "Pos100419"
> "Pos100420"
> > "position"      "r"
> > [13] "result"        "sel"           "select"        "Trad100415"
> > "Trad100416"    "Trad100419"    "Trad100420"    "trade"         "tt"
> > "w"
> >
> >>   l<-list(grep("DailyPL",ls(),value=T))   #create a list of elements
> with
> > DailyPL in the name
> A list of their names, not the elements themselves.
> >
> >> l
> > [[1]]
> > [1] "DailyPL100416" "DailyPL100419" "DailyPL100420"
> >
> >> DF<-do.call("rbind",l)
> >> DF
> >     [,1]            [,2]            [,3]
> > [1,] "DailyPL100416" "DailyPL100419" "DailyPL100420"
> >
> > That's not what I want! I expect "DF" to be a data.frame binded by
> row.
> It is binded by row, but it is from the list you fed it, which is just
> a list with a single element of character strings.
> >
> > I suspect there is an issue with get() or assign(), or something like
> that.
> >
> > Any help is appreciated.
> 
> I am sure there is a neater solution, but something like this should
> work where envir=whether the objects actually are.
> 
> do.call(rbind, mget(grep("DailyPL",ls(),value=TRUE),envir=.GlobalEnv))
> 
> Best regards,
> 
> Josh
> 
> >
> >> -----Original Message-----
> >> From: arnaud Gaboury [mailto:arnaud.gaboury at gmail.com]
> >> Sent: Monday, May 24, 2010 2:55 PM
> >> To: r-help at r-project.org
> >> Cc: 'arnaud Gaboury'
> >> Subject: writing function:loop and rbind
> >>
> >> Dear group,
> >>
> >> I have a function, let's call it myfun, wich give me a list of
> result:
> >> R1,R2,R3...
> >> There is a loop in this function to get my results. Here is the
> >> structure of
> >> my function:
> >>
> >> Myfun<-function()
> >>
> >> {
> >> For (i in X ){
> >>
> >> -----------instructions---------
> >>
> >> Ri
> >>
> >> {
> >> {
> >>
> >> All Results (R1,R2...) are Data.frame. As a final result (call it
> >> "Final"),
> >> I need to rbind all these dataframe. One solution could be to create
> >> another
> >> loop, but I think I can avoid it. How can I add a line like this :
> >> Final<-rbind(R1,R2...) using the i parameter? Another solution could
> >> may be
> >> to create a list of all my results, then apply rbind to the list?
> >>
> >> Any idea would be appreciated.
> >>
> >> TY in advance
> >
> > ______________________________________________
> > 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.
> >
> 
> 
> 
> --
> Joshua Wiley
> Senior in Psychology
> University of California, Riverside
> http://www.joshuawiley.com/



More information about the R-help mailing list