[R] Still avoiding loops

Jacques VESLOT jacques.veslot at cirad.fr
Wed Jan 26 08:40:19 CET 2005


Dear all,

I have a matrix X with 47 lines and say 500 columns - values are in {0,1}.
I'd like to compare lines.

For that, I first did:

for (i in 1:(dim(X)[1]-1))
for (j in (i+1):dim(X)[1]) {
	Y <- X[i,]+Y[j,]
	etc.

but, since it takes a long time, I would prefer avoding loops;
for that, my first idea was to add this matrix:

X1=X[,rep(1:46,46:1)]

to this one:

res=NULL
for (i in (2:47)) res=c(res,i:47)

X2=X[,res]

(Is it a nice alternative way ?)
Is there a way to create the second matrix X2 without a loop, such as for X1
?

Thanks in advance,

Jacques VESLOT




More information about the R-help mailing list