[R] problems with generation of quantiles under For ()

David Winsemius dwinsemius at comcast.net
Mon May 17 14:51:50 CEST 2010


On May 16, 2010, at 6:16 PM, Agustín Muñoz M. (AMFOR) wrote:

> Dear, I want to make an application to calculate quantile within a  
> For()
>
> I tried the following without success:
>
> ej.
>
> date
>
> p_val <- matrix(sample(10, 1000, replace=TRUE), 200,5)
>
> test 1
> rr <- paste("p_val$",names(p_val[1]), sep="")

p_val does not have any names>

names(matrix(1:4, ncol=2))
NULL

> quant <- quantile(rr, probs = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90,
> 100)/100, na.rm=FALSE, type=1)
>
> test 2
> rr <- noquote(paste("p_val$",names(p_val[1]), sep=""))
> quant <- quantile(rr, probs = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90,
> 100)/100, na.rm=FALSE, type=1)
>
> test 3
> quant <- quantile(p_val[1], probs = c(0, 10, 20, 30, 40, 50, 60, 70,  
> 80,
> 90, 100)/100, na.rm=FALSE, type=1)

Your argument is a single number:

 > matrix(1:4, ncol=2)[1]
[1] 1

>
>
> The only thing that works for me is:
>
> quant <- quantile(p_val$1, probs = c(0, 10, 20, 30, 40, 50, 60, 70,  
> 80,
> 90, 100)/100, na.rm=FALSE, type=1)
>
> but the evil, he will not let me do this for a dynamic For()

I think more details are needed. You have provide neither full code,  
not an error message for you failed attempts. You probably want to  
work with:

p_val[, i]

and should test it with

p_val <- matrix(sample(10, 1000, replace=TRUE), 200,5)
 > quant <- quantile(p_val[,1] , probs = c(0, 10, 20, 30, 40, 50, 60,  
70, 80,
+ 90, 100)/100, na.rm=FALSE, type=1)
 >
 > quant
   0%  10%  20%  30%  40%  50%  60%  70%  80%  90% 100%
    1    1    2    3    4    6    7    8    9   10   10

The moral: Avoid using the "$" operator for looping tasks. Use the  
indexing "[" operator using the [,i] or [,1] forms instead.

>
>
> I hope I can help, as they always do.
>
> from now, thank you very much.
>
> Atte.
>
> Agustín
>
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at 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.

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list