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 bla bla ... for (i in 1:Rep) { res1 <- fun1(....) res2 <- fun2(res1,....) output [index,1]<- res2$something index <- index+1 } return(output) } apply(exp.des, 1, myfun) [,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 Lorenzo Cattarino PhD Candidate (Confirmed) Landscape Ecology and Conservation Group Centre for Spatial Environmental Research School of Geography, Planning and Environmental Management The University of Queensland Brisbane, Queensland, 4072, Australia Telephone 61-7-3365 4370, Mobile 0410884610 Email l.cattar...@uq.edu.au Internet http://www.gpem.uq.edu.au/cser <http://www.gpem.uq.edu.au/cser> [[alternative HTML version deleted]] ______________________________________________ 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.