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

Lorenzo Cattarino l.cattarino at uq.edu.au
Tue May 18 04:30:47 CEST 2010


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