Re: [R] apply is making me crazy...

2011-07-28 Thread Dennis Murphy
Hi Gene: I would encourage you to take some time to write a general-purpose function that does exception handling and deals with one-column matrices properly. Since you have nested lists of matrices, operations like lapply() and perhaps more usefully, rapply(), could well be productive. Dennis O

Re: [R] apply is making me crazy...

2011-07-28 Thread Gene Leynes
Yes, I meant to say drop=FALSE Also, I made a mistake in my "desired answer" structure example, sorry for that confusion. The apply results when margin=1 are very unintuitive. That transposition issue has caused me numerous headaches. I think it's a design error, but that changing it would be a

Re: [R] apply is making me crazy...

2011-07-28 Thread Gene Leynes
Dennis, ninety Thanks, I did try almost exactly the same thing. I decided it was too complicated, especially since I have a whole mess of functions I want to use this way. You see, I usually work with lists of lists of matrices that are dimensioned by simulations X time, so there's usually a one

Re: [R] apply is making me crazy...

2011-07-28 Thread David Winsemius
On Jul 28, 2011, at 3:13 PM, Gene Leynes wrote: Very clever (as usual)… It works, but since I wanted to switch the rows and columns, which would require this: answer.slightly.clumsy = lapply(exampBad, function(x) matrix(apply(x ,1, cumsum), ncol=nrow(x))) However, with a slight m

Re: [R] apply is making me crazy...

2011-07-28 Thread Gene Leynes
Very clever (as usual)… It works, but since I wanted to switch the rows and columns, which would require this: answer.slightly.clumsy = lapply(exampBad, function(x) matrix(apply(x ,1, cumsum), ncol=nrow(x))) However, with a slight modification of your code I can use a wrapper function f

Re: [R] apply is making me crazy...

2011-07-28 Thread David Winsemius
On Jul 28, 2011, at 12:31 PM, Gene Leynes wrote: (As I mentioned in my other reply to Dennis, I think I'll stick with for loops, but I wanted to respond.) By "almost does it" I meant that using as.matrix helps because it puts the vector into a column, that "almost does it” because half t

Re: [R] apply is making me crazy...

2011-07-28 Thread Gene Leynes
(As I mentioned in my other reply to Dennis, I think I'll stick with for loops, but I wanted to respond.) By "almost does it" I meant that using as.matrix helps because it puts the vector into a column, that "almost does it” because half the problem is that the output is a non dimensional vector

Re: [R] apply is making me crazy...

2011-07-27 Thread Dennis Murphy
Hi: Try this: exampGood = lapply(2:4, function(x) matrix(rnorm(10 * x), ncol = x)) exampBad = lapply(1:3, function(x) matrix(rnorm(10 * x), ncol = x)) csfun <- function(m) { if(ncol(m) == 1L) {return(m)} else { t(as.matrix(apply(m, 1, cumsum))) } } lapply(exampGood, csfun) lapply(e

Re: [R] apply is making me crazy...

2011-07-27 Thread David Winsemius
On Jul 27, 2011, at 7:44 PM, Gene Leynes wrote: > David, > > Thanks for the suggestion, but I think your answer only works > because I was printing the wrong thing (because apply with margin=1 > transposes the results, And if you want to change that, then the t() function is readily at ha

Re: [R] apply is making me crazy...

2011-07-27 Thread Gene Leynes
David, Thanks for the suggestion, but I think your answer only works because I was printing the wrong thing (because apply with margin=1 transposes the results, something I always forget). Check this to see what I mean: str(answerGood) str(answerBad) Adding "as.matrix" is interesting and

Re: [R] apply is making me crazy...

2011-07-27 Thread David Winsemius
On Jul 27, 2011, at 6:22 PM, Gene Leynes wrote: I have tried a lot of ways around this, but I can't find a way to make apply work in a generalized way because it causes a failure whenever reduces the dimensions of its output. The following example is easier to understand than the question.

[R] apply is making me crazy...

2011-07-27 Thread Gene Leynes
I have tried a lot of ways around this, but I can't find a way to make apply work in a generalized way because it causes a failure whenever reduces the dimensions of its output. The following example is easier to understand than the question. I wish it had a "drop=TRUE/FALSE" option like the "["