[R] apply is making me crazy...
Dennis Murphy
djmuser at gmail.com
Fri Jul 29 05:39:51 CEST 2011
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
On Thu, Jul 28, 2011 at 9:19 AM, Gene Leynes <gleynes+r at gmail.com> wrote:
> 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 column matrix at
> the bottom of the list. So, I really want a general solution because I use
> it all the time.
>
> For now I'm going to stick with for loops, they work fine and are more clear
> in the unlikely event that anyone else ever looks at my code. The loops
> just take up more space / are harder to read.
>
> for example:
> for(i in 1:nrow(mat))
> mat[i,] = cumsum(mat[i,])/(1:ncol(mat))
>
>
>
> On Wed, Jul 27, 2011 at 9:30 PM, Dennis Murphy <djmuser at gmail.com> wrote:
>>
>> 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(exampBad, csfun)
>>
>> HTH,
>> Dennis
>>
>> On Wed, Jul 27, 2011 at 3:22 PM, Gene Leynes <gleynes+r at gmail.com> 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.
>> >
>> > I wish it had a "drop=TRUE/FALSE" option like the "[" (and I wish I had
>> > found the drop option a year ago, and I wish that I had 1e6 dollars...
>> > Oops,
>> > I mean euros).
>> >
>> >
>> > ## Make three example matricies
>> > exampGood = lapply(2:4, function(x)matrix(rnorm(1000*x),ncol=x))
>> > exampBad = lapply(1:3, function(x)matrix(rnorm(1000*x),ncol=x))
>> > ## Two ways to see what was created:
>> > for(k in 1:length(exampGood)) print(dim(exampGood[[k]]))
>> > for(k in 1:length(exampBad)) print(dim(exampBad[[k]]))
>> >
>> > ## Take the cumsum of each row of each matrix
>> > answerGood = lapply(exampGood, function(x) apply(x ,1,cumsum))
>> > answerBad = lapply(exampBad, function(x) apply(x ,1,cumsum))
>> > str(answerGood)
>> > str(answerBad)
>> >
>> > ## Take the first element of the final column of each answer
>> > for(mat in answerGood){
>> > LastColumn = ncol(mat)
>> > print(mat[1,LastColumn])
>> > }
>> > for(mat in answerBad){
>> > LastColumn = ncol(mat)
>> > print(mat[1,LastColumn])
>> > }
>> >
>> > [[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.
>> >
>
>
More information about the R-help
mailing list