[R] for loops and counters

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Oct 5 15:31:15 CEST 1999


"Peter B. Mandeville" <mandevip at uaslp.mx> writes:

> 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))
> 		}
> 	}

Er, what goes wrong? It seems to work for me (except that you've put
nasty TAB characters at the beginning of the lines, which confuses
readline more than slightly!), provided that k, rms, means, and df are
given sensible values first. In principle, you'd be better off with

pvalue <- numeric(k*(k-1)/2)
r <- 0
for(etc)....

which avoids a lot of memory allocation, but it should work as written.


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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