[R] applying quantile to a list using values of another object as probs

Jim Lemon jim at bitwrit.com.au
Mon May 17 12:26:38 CEST 2010


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