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 almost does it, however the results
are still transposed.

Sorry to be confusing with the initial example.

Here's an updated example (adding as.matrix doesn't make a difference)


## 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))
answerProposed  = lapply(exampBad, function(x) as.matrix(apply(x
,1:1,cumsum)))
str(answerGood)
str(answerBad)
str(answerProposed)

##  Take the first element of the final column of each answer
for(mat in answerGood){
    mat = t(mat)  ## To get back to 1000 rows
    LastColumn = ncol(mat)
    print(mat[2,LastColumn])
}
for(mat in answerBad){
    mat = t(mat)  ## To get back to 1000 rows
    LastColumn = ncol(mat)
    print(mat[2,LastColumn])
}
for(mat in answerProposed){
    mat = t(mat)  ## To get back to 1000 rows
    LastColumn = ncol(mat)
    print(mat[2,LastColumn])
}



On Wed, Jul 27, 2011 at 5:45 PM, David Winsemius <dwinsem...@comcast.net>wrote:

>
> 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.
>>
>> 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))
>>
>
> Try instead:
>
> answerBad  = lapply(exampBad, function(x) as.matrix(apply(x ,1:1,cumsum)))
>
>
> I also find wrapping as.matrix() around vector results inside a print()
> call often makes my console output much more to my liking.
>
>
>    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@r-project.org mailing list
>> https://stat.ethz.ch/mailman/**listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide http://www.R-project.org/**
>> posting-guide.html <http://www.R-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>
>
> David Winsemius, MD
> West Hartford, CT
>
>

        [[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.

Reply via email to