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)) } } Thank you. Peter B. -- Peter B. Mandeville mandevip at deimos.tc.uaslp.mx Jefe del Depto. de Inform?tica y Bioestad?stica rpe1531 at pasteur.fmed.uaslp.mx Facultad de Medicine Tel: 48 26-23-45 ext. 232 Universidad Aut?noma de San Luis Potos? Fax: 48 28-23-52 Av. V. Carranza 2405 Col. Los Filtros Apartado Postal 145 San Luis Potos?, S.L.P. 78210 M?xico -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- 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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
"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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
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 _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._