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.cattarino@uq.edu.au Internet http://www.gpem.uq.edu.au/cser <http://www.gpem.uq.edu.au/cser> [[alternative HTML version deleted]]
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
On Wed, Aug 11, 2010 at 4:51 PM, Lorenzo Cattarino <l.cattarino at uq.edu.au> 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 restartsYes, this is quite expected. You define index outside of your function and for loop, and proceed to only update it in your for loop which is in your function.> from 1, without being updated (like I am trying to make it doing). Here > is an example of what I am talking about:What follows is not really an example. There is no real data, which while annoying could be overcome, but we also do not have your function. Last I heard, present or future Greg (I mix them up) is working on a ESP package, but I am pretty certain it has not been released yet. If it is not too terribly important that the results of apply() be in the matrix as you want, why not simply reformat the data you have? mymatrix <- matrix(c(1:15, rep(NA, 30)), ncol = 3, byrow = TRUE) mymatrix #Grab the first 5 rows of data and reformat them matrix(mymatrix[1:5, ], ncol = 1) Since apply() will combine all of its results anyways, I would get rid of index and just have an internal index in the for loop. Cheers, Josh> > > > 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.cattarino at uq.edu.au > > Internet http://www.gpem.uq.edu.au/cser <http://www.gpem.uq.edu.au/cser> > > > > > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > 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 Ph.D. Student, Health Psychology University of California, Los Angeles http://www.joshuawiley.com/