[R] applying quantile to a list using values of another object as probs
Dan Davison
davison at stats.ox.ac.uk
Tue May 18 05:32:53 CEST 2010
"Lorenzo Cattarino" <l.cattarino at uq.edu.au> writes:
> Hi Jim,
>
> Thanks for your reply. Your codes does work but I was hoping to find a
> way to use lapply and avoid the for loop.
>
> Lorenzo
>
>
> -----Original Message-----
> From: Jim Lemon [mailto:jim at bitwrit.com.au]
> Sent: Monday, 17 May 2010 8:27 PM
> To: Lorenzo Cattarino
> Cc: r-help at r-project.org
> Subject: Re: [R] applying quantile to a list using values of another
> object as probs
>
> On 05/17/2010 06:01 PM, Lorenzo Cattarino wrote:
>> Hi r-users,
>>
>> I have a matrix B and a list of 3x3 matrices (mylist). I want to
>> calculate the quantiles in the list using each of the value of B as
>> probabilities.
It's a little confusing, because it isn't clear why the elements of
mylist are matrices, nor why B is a matrix. I.e. why aren't these things
just dimensionless vectors? However if you really do want to ignore the
row/column information then perhaps what you're looking for is
lapply(mylist, quantile, probs=B)
[[1]]
26.55087% 37.21239% 57.28534% 90.82078% 20.16819% 89.83897% 94.46753% 66.07978% 62.9114%
-0.2191315 0.3738468 0.5389231 1.2277025 -0.4274793 1.1973174 1.3405621 0.6223309 0.5811310
6.178627% 20.59746% 17.65568%
-1.4270686 -0.4166326 -0.4909661
[[2]]
26.55087% 37.21239% 57.28534% 90.82078% 20.16819% 89.83897% 94.46753%
-0.004930323 0.072476814 0.703609732 0.925581428 -0.027300847 0.923628895 0.932833742
66.07978% 62.9114% 6.178627% 20.59746% 17.65568%
0.793329524 0.783422677 -1.028244961 -0.026313767 -0.033078300
[[3]]
26.55087% 37.21239% 57.28534% 90.82078% 20.16819% 89.83897% 94.46753% 66.07978% 62.9114%
-0.1492189 -0.1040074 0.2025300 0.8161114 -0.2803999 0.7580782 1.0316644 0.3963404 0.3886679
6.178627% 20.59746% 17.65568%
-0.9801188 -0.2693299 -0.3451936
Dan
>>
>>
>>
>> The codes I wrote are:
>>
>>
>>
>> B<- matrix (runif(12, 0, 1), 3, 4)
>>
>> mylist<- lapply(mylist, function(x) {matrix (rnorm(9), 3, 3)})
>>
>>
>>
>> for (i in 1:length(B))
>>
>> {
>>
>> quant<- lapply (mylist, quantile, probs=B[i])
>>
>> }
>>
>>
>>
>> But quant returned the quantiles calculated using only the last value
>> ([3,3]) of the matrix B.
>>
>>
> Hi Lorenzo,
> This works for me:
>
> B<-matrix (runif(12,0,1),3,4)
> mylist<-list()
> for(i in 1:3) mylist[[i]]<-matrix(rnorm(9),3,3)
> myq<-list()
> for(i in 1:3)myq[[i]]<-quantile(mylist[[i]],probs=B[i,])
>
> Although looking at your example, I may have misunderstood what you want
>
> the result to be.
>
> Jim
More information about the R-help
mailing list