Re: [S] Looping

Alberto Murta (amurta@ipimar.pt)
Fri, 21 Aug 1998 18:34:08 +0100


The function below is not very elegant but avoids explicit loops. It
seems not to work with large vectors (length around 3000):

> my.func
function(x)
{
x <- as.data.frame(matrix(x, 1, length(x)))
names(x) <- as.character(c(1:length(x)))
vecnames <- as.integer(names(x[, x == 1]))
lenvec <- length(vecnames)
vec2 <- rep(vecnames, lenvec)
vec3 <- rep(vecnames, rep(lenvec, lenvec))
vec4 <- vec2 - vec3
vec4 <- vec4[vec4 >= 0] + 1
tabulate(vec4)
}
> x
[1] 1 0 1 1 2 1 2 1 0 1
> my.func(x)
[1] 6 1 4 2 2 2 1 2 0 1

Besides, you may have to change your object.size options:

> x<-rep(x,200)
> length(x)
[1] 2000
> options(object.size=20e+006)
> dos.time(my.func(x))
[1] 31.97

-- 
	Alberto G. Murta
Instituto de Investigacao das Pescas e do Mar (IPIMAR)
Departamento de Recursos Marinhos
Avenida Brasilia	    telefone:  +351 1 3027062
1400 Lisboa Portugal	         fax:  +351 1 3015948
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news