Re: [R] writing function:loop and rbind

From: arnaud Gaboury <arnaud.gaboury_at_gmail.com>
Date: Mon, 24 May 2010 17:23:42 +0200

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@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/



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. Received on Mon 24 May 2010 - 15:27:08 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Mon 24 May 2010 - 15:50:23 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive