[R] for loops and counters

Martyn Plummer plummer at iarc.fr
Tue Oct 5 15:51:13 CEST 1999


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



More information about the R-help mailing list