I am sure this si a simple problem but the solution is evading me. I have a list of matrices all with the same number of columns but different number of rows. The first two columns label the row. The labels are allways the same for the same row numbers, just some matricies have more rows. For example using 3 column matrices...> q.1 <- function(r){return(cbind(seq(0, 10, by=1)[1:r], seq(10, 30,by=2)[1:r], runif(r)))}> sapply(q, q.1)[[1]] [,1] [,2] [,3] [1,] 0 10 0.5399220 [2,] 1 12 0.1551015 [3,] 2 14 0.9664470 [[2]] [,1] [,2] [,3] [1,] 0 10 0.09678172 [2,] 1 12 0.75177116 [3,] 2 14 0.59927159 [4,] 3 16 0.18472215 [[3]] [,1] [,2] [,3] [1,] 0 10 0.6343689 [2,] 1 12 0.8121039 Given such a list I would like to create a matrix: 0 10 mean(ThisCol for this row) 1 12 mean(ThisCol for this row) 2 14 mean(ThisCol for this row) 3 16 mean(ThisCol for this row) I can loop using a for loop but I would like to use apply but I have no idea how to get it to work. If I could pass arguments by reference to a function it would be easy but as far as I can tell there is only pass by value. cheers Worik [[alternative HTML version deleted]]
Henrique Dallazuanna
2010-Dec-15 00:34 UTC
[R] Modifying values outside a function using "apply".
Try this: l <- sapply(c(4, 3, 2), q.1) aggregate(V3 ~ V1 + V2, as.data.frame(do.call(rbind, l)), mean) On Tue, Dec 14, 2010 at 9:19 PM, Worik R <worikr@gmail.com> wrote:> I am sure this si a simple problem but the solution is evading me. > > I have a list of matrices all with the same number of columns but different > number of rows. The first two columns label the row. The labels are > allways the same for the same row numbers, just some matricies have more > rows. > > For example using 3 column matrices... > > > q.1 <- function(r){return(cbind(seq(0, 10, by=1)[1:r], seq(10, 30, > by=2)[1:r], runif(r)))} > > sapply(q, q.1) > [[1]] > [,1] [,2] [,3] > [1,] 0 10 0.5399220 > [2,] 1 12 0.1551015 > [3,] 2 14 0.9664470 > > [[2]] > [,1] [,2] [,3] > [1,] 0 10 0.09678172 > [2,] 1 12 0.75177116 > [3,] 2 14 0.59927159 > [4,] 3 16 0.18472215 > > [[3]] > [,1] [,2] [,3] > [1,] 0 10 0.6343689 > [2,] 1 12 0.8121039 > > Given such a list I would like to create a matrix: > > 0 10 mean(ThisCol for this row) > 1 12 mean(ThisCol for this row) > 2 14 mean(ThisCol for this row) > 3 16 mean(ThisCol for this row) > > I can loop using a for loop but I would like to use apply but I have no > idea > how to get it to work. If I could pass arguments by reference to a > function > it would be easy but as far as I can tell there is only pass by value. > > > cheers > Worik > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]