Skip to content

to replace the for loop

2 messages · Arnaud Michel, Berend Hasselman

#
Hello

I would like to replace the for loop this below

T <- as.matrix(T)
for(i in 1: nrow(TEMP)){
for(j in 1: nrow(TEMP)){if (i <= j) T[i, j] <- 0 }}

I don't find the function in the doc.
Thanks in advance for your help.
#
On 14-12-2013, at 08:01, Arnaud Michel <michel.arnaud at cirad.fr> wrote:

            
Your code is mangled.
We don?t know what T is.
You refer to TEMP in the for loop but you most likely meant T.

Shouldn?t the second nrow in your for loop be ncol?
Search with ??uppertri and you?ll find lower.tri in base with sufficient description to have a look at that entry.
Depending on what packages you have, you may find more.
T[col(T)>=row(T)] <- 0
T[upper.tri(T,diag=TRUE) ] <- 0

Berend