Re: [R] how to get a series of results by "loop"

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sat 27 May 2006 - 05:03:36 EST

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@gmail.com> wrote:
> I donot know how to do it remainly.
> Perhaps my question is not clear.
> For example,
>
> test=data.frame(sp=paste(c("sp"),1:100,sep=""),x=rnorm(100),y=rnorm(100))
>
> # I want to get the data in the circle of radius = 1 in every point, and I
> want to add a sign (e.g. sp1,sp2....) in order that I can distinguish
> different groups
> # if I want to choose a point, I can do it like this
>
> r = 1 # radius
> attach(test)
> point100=test[(x-x[100])^2+(y-y[100])^2<=r^2,]
> result100=data.frame
> (note=paste(c("p"),rep(100,length(point100$sp)),sep=""),point100)
> detach(test)
>
> But if I want to do all points in "test", I donot know how to do it. I try
> to do it by using "for", but it doesnot work.
> Can you give me some advices?
> thanks!
> jian zhang
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Sat May 27 05:12:34 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 27 May 2006 - 06:10:29 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.