Lorenzo Cattarino
2010-May-17 08:01 UTC
[R] applying quantile to a list using values of another object as probs
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.
What am I doing wrong?
Thank you for your help
Lorenzo
[[alternative HTML version deleted]]
Jim Lemon
2010-May-17 10:26 UTC
[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
Lorenzo Cattarino
2010-May-18 02:30 UTC
[R] applying quantile to a list using values of another object as probs
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
Lorenzo Cattarino
2010-May-18 04:45 UTC
[R] applying quantile to a list using values of another object as probs
Hi Jorge
It worked!
lapply(1:length(mylist), function(i) quantile(mylist[[i]], probs B[i])) #like
yours but without coma after i
[[1]]
99.62995%
0.7989808
[[2]]
55.33357%
-0.653456
[[3]]
72.30253%
-0.4872166
[[4]]
39.69968%
-1.138819
[[5]]
29.84813%
-1.235156
[[6]]
50.11912%
-0.7583491
[[7]]
65.88863%
-0.5016644
[[8]]
36.94447%
-1.220691
[[9]]
80.69874%
-0.1123578
[[10]]
58.53488%
-0.589059
[[11]]
73.03351%
-0.48557
[[12]]
24.12637%
-1.258081
I am not quite sure why it worked. I wonder why a simpler
lapply(1:length(mylist), quantile, probs=B[i]) does not work and just
gives me the following:
[[1]]
24.12637%
1
[[2]]
24.12637%
2
[[3]]
24.12637%
3
[[4]]
24.12637%
4
[[5]]
24.12637%
5
[[6]]
24.12637%
6
[[7]]
24.12637%
7
[[8]]
24.12637%
8
[[9]]
24.12637%
9
[[10]]
24.12637%
10
[[11]]
24.12637%
11
[[12]]
24.12637%
12
Thank you for your help
Lorenzo
From: Jorge Ivan Velez [mailto:jorgeivanvelez@gmail.com]
Sent: Tuesday, 18 May 2010 12:37 PM
To: Lorenzo Cattarino
Subject: Re: [R] applying quantile to a list using values of another
object as probs
Hi Lorenzo,
How about this?
lapply(1:length(mylist), function(i) quantile(mylist[[i]], probs B[i,]))
HTH,
Jorge
On Mon, May 17, 2010 at 10:30 PM, Lorenzo Cattarino <> wrote:
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@bitwrit.com.au]
Sent: Monday, 17 May 2010 8:27 PM
To: Lorenzo Cattarino
Cc: r-help@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
______________________________________________
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.
[[alternative HTML version deleted]]