Skip to content
Prev 93317 / 398500 Next

how to get a series of results by "loop"

Try this:

set.seed(1)
r <- .1
n <- 5
test <- data.frame(sp = paste("sp", 1:n, sep = ""), x= rnorm(n), y = rnorm(n))
r <- 1 # radius
f <- function(i) cbind(orig = test[i,1], subset(test, (x - x[i])^2 +
(y - y[i])^2 < r^2))
do.call("rbind", lapply(1:n, f))
On 5/26/06, zhang jian <jzhang1982 at gmail.com> wrote: