François Pinard
2006-Jul-16 01:34 UTC
[Rd] Matrices given to pt? [was: [R] for loops and counters]
Hi, people. I was a bit intrigued by the message quoted below. Indeed, if pt() is given a matrix, it returns a matrix. Should this feature be documented? ?pt speaks about "a vector of quantiles", and says nothing about the type of what it returns. The same might presumably apply to other distribution-related functions. ----- Forwarded message from Martyn Plummer <plummer at iarc.fr> ----- From: Martyn Plummer <plummer at iarc.fr> Date: Thu Apr 10 10:07:01 2003 Subject: [R] for loops and counters Peter, You didn't say what goes wrong, but you mind find this version more useful tstat <- (matrix(means, k,k, byrow=TRUE) - matrix(means, k,k,byrow=FALSE))/rms pvalue <- 2*(1-pt(abs(tstat), df=df)) pvalue[upper.tri(pvalue)] S is designed to allow you to work with "whole objects", which means that for loops can (and should) be avoided. Martyn On 05-Oct-99 Peter B. Mandeville wrote:> I am trying to assign values to a vector (pvalue). Similar code works in C > but not in R. What am I doing wrong?> r <- pvalue <- 0 > for(i in (1:(k-1))){ > for(j in (i+1):k){ > r <- r+1 > tstat <- (means[i]-means[j])/rms > pvalue[r] <- 2*(1-pt(abs(tstat),df)) > } > }-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ ----- End forwarded message ----- -- Fran?ois Pinard http://pinard.progiciels-bpi.ca
Gabor Grothendieck
2006-Jul-16 04:03 UTC
[Rd] Matrices given to pt? [was: [R] for loops and counters]
I think what is happening is that it is preserving attributes of the input. e.g.> x <- 1:3 > attr(x, "A") <- 23 > pt(x, 3)[1] 0.8044989 0.9303370 0.9711656 attr(,"A") [1] 23 Note that attribute A was preserved. Since matrices are just vectors with a dim attribute that would imply the result. I agree that it would be a good idea to document it if it is intended. On 7/15/06, Fran?ois Pinard <pinard at iro.umontreal.ca> wrote:> Hi, people. > > I was a bit intrigued by the message quoted below. Indeed, if pt() is > given a matrix, it returns a matrix. Should this feature be documented? > ?pt speaks about "a vector of quantiles", and says nothing about the > type of what it returns. > > The same might presumably apply to other distribution-related functions. > > ----- Forwarded message from Martyn Plummer <plummer at iarc.fr> ----- > > From: Martyn Plummer <plummer at iarc.fr> > Date: Thu Apr 10 10:07:01 2003 > Subject: [R] for loops and counters > > Peter, > > You didn't say what goes wrong, but you mind find this version more useful > > tstat <- (matrix(means, k,k, byrow=TRUE) - matrix(means, k,k,byrow=FALSE))/rms > pvalue <- 2*(1-pt(abs(tstat), df=df)) > pvalue[upper.tri(pvalue)] > > S is designed to allow you to work with "whole objects", which means that > for loops can (and should) be avoided. > > Martyn > > On 05-Oct-99 Peter B. Mandeville wrote: > > I am trying to assign values to a vector (pvalue). Similar code works in C > > but not in R. What am I doing wrong? > > > r <- pvalue <- 0 > > for(i in (1:(k-1))){ > > for(j in (i+1):k){ > > r <- r+1 > > tstat <- (means[i]-means[j])/rms > > pvalue[r] <- 2*(1-pt(abs(tstat),df)) > > } > > } > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > > ----- End forwarded message ----- > > -- > Fran?ois Pinard http://pinard.progiciels-bpi.ca > > ______________________________________________ > R-devel at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-devel >
Prof Brian Ripley
2006-Jul-18 10:29 UTC
[Rd] Matrices given to pt? [was: [R] for loops and counters]
There is a general rule that scalar operations (that is functions that work element-by-element and return a scalar result) preserve attributes including names, dims and dimnames. Similarly, there is a general rule about binary operations and attributes. These things should definitely be in the `R Language Definition', but that languishes as a draft. I am writing up comments on R internals (currently an appendix on `Writing R Extensions', and this will go there (the section on attributes is current work). On Sat, 15 Jul 2006, Fran?ois Pinard wrote:> Hi, people. > > I was a bit intrigued by the message quoted below. Indeed, if pt() is > given a matrix, it returns a matrix. Should this feature be documented? > ?pt speaks about "a vector of quantiles", and says nothing about the > type of what it returns. > > The same might presumably apply to other distribution-related functions. > > ----- Forwarded message from Martyn Plummer <plummer at iarc.fr> ----- > > From: Martyn Plummer <plummer at iarc.fr> > Date: Thu Apr 10 10:07:01 2003 > Subject: [R] for loops and counters > > Peter, > > You didn't say what goes wrong, but you mind find this version more useful > > tstat <- (matrix(means, k,k, byrow=TRUE) - matrix(means, k,k,byrow=FALSE))/rms > pvalue <- 2*(1-pt(abs(tstat), df=df)) > pvalue[upper.tri(pvalue)] > > S is designed to allow you to work with "whole objects", which means that > for loops can (and should) be avoided. > > Martyn > > On 05-Oct-99 Peter B. Mandeville wrote: > > I am trying to assign values to a vector (pvalue). Similar code works in C > > but not in R. What am I doing wrong? > > > r <- pvalue <- 0 > > for(i in (1:(k-1))){ > > for(j in (i+1):k){ > > r <- r+1 > > tstat <- (means[i]-means[j])/rms > > pvalue[r] <- 2*(1-pt(abs(tstat),df)) > > } > > } > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > > ----- End forwarded message ----- > >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595