Skip to content

writing function:loop and rbind

5 messages · Arnaud Gaboury, Joshua Wiley

#
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
#
One solution is to create a list, then do.call.
Here is my environment:
[1] "DailyPL100416" "DailyPL100419" "DailyPL100420" "l"             "ll"
"PLglobal"      "Pos100415"     "Pos100416"     "Pos100419"     "Pos100420"
"position"      "r"            
[13] "result"        "sel"           "select"        "Trad100415"
"Trad100416"    "Trad100419"    "Trad100420"    "trade"         "tt"
"w"
DailyPL in the name
[[1]]
[1] "DailyPL100416" "DailyPL100419" "DailyPL100420"
[,1]            [,2]            [,3]           
[1,] "DailyPL100416" "DailyPL100419" "DailyPL100420"

That's not what I want! I expect "DF" to be a data.frame binded by row.

I suspect there is an issue with get() or assign(), or something like that.

Any help is appreciated.
#
On Mon, May 24, 2010 at 7:00 AM, arnaud Gaboury
<arnaud.gaboury at gmail.com> wrote:
A list of their names, not the elements themselves.
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 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

  
    
#
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
#
Do you think there is a way to add somewhere the argument row.names=NULL ? Or should I have to write another line to remove the row.names?