On Aug 11, 2010, at 7:51 PM, Lorenzo Cattarino wrote:

Hi R-users,
I have a function (myfun) that I want to apply to the rows of a matrix.
Basically, "myfun" takes the values from the matrix ("exp.des"), which
represent the different combinations of my experimental design, and pass them as arguments to some other functions (fun1 and fun2). As I want to
replicate the results of fun1 and fun2 a certain number of time (e.g.
5), I have a for loop within myfun.

I would like to store the results of each iteration of fun1 and fun2 in a separate matrix ("output") but I have some problems telling R which is the position of "output" where to store the results. It seems that every time "myfun" is applied to each row of "exp.des", the position restarts from 1, without being updated (like I am trying to make it doing). Here
is an example of what I am talking about:

output <- matrix (, 15, 1)

index <- 1
myfun <- function(x)
{
bla # goes against the Posting Guide directive to produce reproducible code
  bla #  ...  if it doesn't matter than leave it out or use "#"
  bla
  ...
  for (i in 1:Rep)  #  Rep not defined
# the blank lines are also a barrier to comprehension
 {
    res1 <- fun1(....) # more useless junk
   res2 <- fun2(res1,....)
   output [index,1]<- res2$something
   index <- index+1
  }
return(output) # so you will get one of "output" per line of matrix or dataframe

# But /// ... it does not (or at least should not in my understanding of scoping) go into in that "output" object that you defined outside the "myfun" function.
}
apply(exp.des, 1, myfun)

Would do absolutely nothing since you did not assign the output of the "output's from apply() to any object. Lexical scoping. If you do not understand the term then do some more reading.


Learn to post in plain text please. This is a plain text mailing list. Ix-nay on the T-M-L-Hay.
     [,1] [,2] [,3]

[1,]   23    5   99

[2,]    6   45   18

[3,]    1   65    9

[4,]    9   10   45

[5,]    3   30    9

[6,]   NA   NA   NA

[7,]   NA   NA   NA

[8,]   NA   NA   NA

[9,]   NA   NA   NA

[10,]   NA   NA   NA

[11,]   NA   NA   NA

[12,]   NA   NA   NA

[13,]   NA   NA   NA

[14,]   NA   NA   NA

[15,]   NA   NA   NA


I would like to have just one column with the three groups of five
values.

Many thanks
Lorenzo
\

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to