Hello,
I would like to know more about how to use the "apply" family and have
attempted to convert nested "for" loops in example code from Contributed
Documentation ("The Friendly Beginners' R Courseā by Toby Marthews (ZIP,
2007-03-01)") to an "apply" function(s). The relevant code is:
distances=c(51,65,175,196,197,125,10,56) #distances of 8 houses from the
town centre in m
bearings=c(10,8,210,25,74,128,235,335) #bearings of the houses in degrees
xpos=distances*sin(bearings*pi/180) #in sin and cos the argument
MUST be in radians
ypos=distances*cos(bearings*pi/180)
numpoints=length(distances)
nnd=rep(sqrt(2*400*400),times=numpoints) #start with the maximum
possible distance
for (i in 1:numpoints) {
for (j in 1:numpoints) {
if (i!=j) {
diffx=abs(xpos[i]-xpos[j])
diffy=abs(ypos[i]-ypos[j])
nd=sqrt((diffx^2)+(diffy^2))
if (nd
______________________________________________
[email protected] 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.