[R] random point in a circle centred in a geographical position

2006-10-07 Thread Albert Picado
Dear List members

I am trying to find a way to generate a random point in a circle centred in a 
geographical location.

So far I have used the following formula (see code below):
random_x = original_x + radius*cos(angle)
random_y = original_y + radius*sin(angle)
where radius is a random number between 0 and the radius of the circle and
angle is between 0 and 360 degrees

The code bellow works fine however some people argue that this method will 
produce uneven locations… I really do not understand why.

Another option will be to use the “rejection method”: generate points inside a 
box and reject points that fall outside the circle. However I think this is 
much complicate… or at least I don’t know how to programme it.

So,
1. Is there any package that generates random points in a circle? (I couldn’t 
find any)
2. Do you think the code bellow produces uneven locations?
3. Any suggestions to programme the “rejection method”? Maybe someone has 
already used it...

Cheers

albert

#Code random location in a circle
# Generate UTM coordinates (original location)
 x_utm-c(289800)
 y_utm-c(4603900)
 coord-as.data.frame(cbind(x_utm,y_utm))
# Generate a random radius with a maximum distance of 1000 meters from the 
original location.
radius-runif(1,0,1000)
# Generate a random angle
angle-runif(1,0,360)
#Generate a random x coordinate
x_rdm-coord$x_utm[1]+radius*cos(angle)
#Generate a random y coordinate
y_rdm-coord$y_utm[1]+radius*sin(angle)
result-as.data.frame(cbind(x_rdm,y_rdm, x_utm, y_utm))
# We can calculate the distance between the original and the random location
 result$dist_m-sqrt(((coord$x_utm-result$x_rdm)^2+(coord$y_utm-result$y_rdm)^2))
result

__
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
and provide commented, minimal, self-contained, reproducible code.


[R] adding columns to a table after a loop

2006-08-12 Thread Albert Picado
Dear list,

I am trying to find the way to add columns to a table
(or a matrix) after a loop. The result of this loop is
an array and I would like to get all the results in a
single table once the loop is finish. I have
reproduced a simplified example below:

 a- 1
 b - matrix()
 while (a = 4) {
+ b-rnorm(10)
+ a- a +1
+ }

# I have tried several methods but without succes so
far. 

 result - as.data.frame(cbind(b))
 result
 b
1  -0.03250661
2  -0.59823770
3   1.58120471
4   0.41086546
5   0.78959090
6   1.23587125
7   0.83427190
8   1.09035581
9   0.11331056
10  0.25267231

the result above is not what I am looking for because
it shows only the results from the last operation in
the loop, I would like to obtain something like the
following.

 result
b1 b2  b3 b4
1  -0.08420826 -0.0637943 -0.83246201 -1.0902384
2   0.40623009 -2.7940096  1.37664973  1.6023967
3  -1.13850505  1.1660669 -0.95962296 -0.7325098
4  -1.06183391  1.1063677  1.67948677 -1.9875475
5  -0.64431067 -0.4843952 -1.30742858  0.5064134
6   0.56729468  1.0860484  0.07651954  0.4380108
7   0.95036177 -0.5328609  1.28954934 -0.2775614
8  -0.17848223  0.5340379 -0.22613700  1.0179886
9   2.93145454 -1.8639607 -0.25478610  1.6619754
10  1.51942415 -0.2051423  0.09144450 -1.6329481

I hope someone can give me a hand

best regards

a





 p5.vert.ukl.yahoo.com uncompressed/chunked Sat Aug 12 13:14:00 GMT 2006

__
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
and provide commented, minimal, self-contained, reproducible code.


[R] Re : adding columns to a table after a loop

2006-08-12 Thread Albert Picado
Dear Uwe,
 
Thanks for your answer. I have been trying your suggestion but I haven't been 
able to solve my problem as I may have simplified in excess my example. The 
real situation is actually much similar to the following:
 
a-1
b-1
i-1
pvalue-matrix()
while(a=4) {
 while(b=10){
 pvalue[i]-rnorm(1)
 i-i+1
 b-b+1
 }
a-a+1
}

To get the desired outcome I have tried two things based on your suggestion:
 
1)
 
a-1
b-1
i-1
pvalue- matrix(ncol=4, nrow=10)
result- matrix()
while(a=4) {
 while(b=10){
 pvalue[i]-rnorm(1)
 i-i+1
 b-b+1
 }
result[,a]-pvalue
a-a+1
}

here I get an error message: 
Error in [-(`*tmp*`, , a, value = c(1.38661984066808, -0.641565430307799,  : 
number of items to replace is not a multiple of replacement length

2)
 
a-1
b-1
i-1
pvalue-matrix(ncol=4, nrow=10)
while(a=4) {
 while(b=10){
 pvalue[i,a]-rnorm(1)
 i-i+1
 b-b+1
 }
a-a+1
}

here what I get is:
 
 pvalue
 [,1] [,2] [,3] [,4]
 [1,] -2.18379100   NA   NA   NA
 [2,] -0.90112392   NA   NA   NA
 [3,] -0.91762163   NA   NA   NA
 [4,]  0.54922398   NA   NA   NA
 [5,]  0.95511590   NA   NA   NA
 [6,]  0.08509518   NA   NA   NA
 [7,] -0.92576835   NA   NA   NA
 [8,]  1.62361191   NA   NA   NA
 [9,]  0.02644486   NA   NA   NA
[10,]  0.32406407   NA   NA   NA

 
I do not know what else to try...
 
best wishes
 
a


- Message d'origine 
De : Uwe Ligges [EMAIL PROTECTED]
À : Albert Picado [EMAIL PROTECTED]
Cc : r-help@stat.math.ethz.ch
Envoyé le : Samedi, 12 Août 2006, 3h57mn 16s
Objet : Re: [R] adding columns to a table after a loop


Albert Picado wrote:
 Dear list,
 
 I am trying to find the way to add columns to a table
 (or a matrix) after a loop. The result of this loop is
 an array and I would like to get all the results in a
 single table once the loop is finish. I have
 reproduced a simplified example below:
 
 a- 1
 b - matrix()
 while (a = 4) {
 + b-rnorm(10)
 + a- a +1
 + }


Staying in your example:

a - 1
b - matrix(nrow=10, ncol=4)
while(a = 4){
 b[,i] - rnorm(10)
 a - a + 1
}

Uwe Ligges



 # I have tried several methods but without succes so
 far. 
 
 result - as.data.frame(cbind(b))
 result
  b
 1  -0.03250661
 2  -0.59823770
 3   1.58120471
 4   0.41086546
 5   0.78959090
 6   1.23587125
 7   0.83427190
 8   1.09035581
 9   0.11331056
 10  0.25267231
 
 the result above is not what I am looking for because
 it shows only the results from the last operation in
 the loop, I would like to obtain something like the
 following.
 
 result
 b1 b2  b3 b4
 1  -0.08420826 -0.0637943 -0.83246201 -1.0902384
 2   0.40623009 -2.7940096  1.37664973  1.6023967
 3  -1.13850505  1.1660669 -0.95962296 -0.7325098
 4  -1.06183391  1.1063677  1.67948677 -1.9875475
 5  -0.64431067 -0.4843952 -1.30742858  0.5064134
 6   0.56729468  1.0860484  0.07651954  0.4380108
 7   0.95036177 -0.5328609  1.28954934 -0.2775614
 8  -0.17848223  0.5340379 -0.22613700  1.0179886
 9   2.93145454 -1.8639607 -0.25478610  1.6619754
 10  1.51942415 -0.2051423  0.09144450 -1.6329481
 
 I hope someone can give me a hand
 
 best regards
 
 a
 
 
 
 
 
  p5.vert.ukl.yahoo.com uncompressed/chunked Sat Aug 12 13:14:00 GMT 2006
 
 __
 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
 and provide commented, minimal, self-contained, reproducible code.

__
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
and provide commented, minimal, self-contained, reproducible code.