Grokbase Groups R r-help June 2005
Hi Mike,

You should think of "vector" sums rather than arithmetic sum. So the mean
is really the direction of the resultant vector of all the unit vectors.
When two unit vectors are exactly opposing each other (as in your example
with angles 45 and 225 degrees), they cancel each other out and the
resultant vector has length zero, and hence the direction is arbitrary and
undefined. However, there are two issues which give rise to seemingly
anomalous results: (1) due to finite numerical precision in computing the
sines and cosines, exact cancellation does not happen, and (2) discontinuity
in atan function due to branch-point (0, 0) and a branch cut along negative
real axis.

For example,
[1] -56.30993247402022
sx <- sin(rad(210)) + sin(rad(30))
cx <- cos(rad(210)) + cos(rad(30))
[1] -1.665334536937735e-16
[1] 1.110223024625157e-16
atan(sx,cx) # this should really be 0
[1] -0.9827937232473291
If you now look at the resultant vector it has components (sin(-0.9828),
cos(-0.9828)) = (-0.83,0.55), which is completely wrong.

However, the above example is only a minor perturbation away from the
following (where the resultant vector is not zero):
deg(circ.mean(c(rad(211),rad(30)))) # note 211 = -149 (mod 360)
[1] -59.49999999999996
which is perfectly alright.

Hope this helps,

Ravi Varadhan, Ph.D.
Assistant Professor, The Center on Aging and Health
Division of Geriatric Medicine and Gerontology
Johns Hopkins University
Ph: (410) 502-2619
Fax: (410) 614-9625
Email: rvaradhan at
-----Original Message-----
From: r-help-bounces at [mailto:r-help-
bounces at] On Behalf Of Michael Peckford
Sent: Tuesday, June 28, 2005 1:35 PM
To: r-help at
Subject: [R] Circular Mean Question

Hi, a question about the circular mean function in the package

Can anyone shed some light on why the circ mean function seems to make
sense for the first 2 set of bearings and then the mean of 225 and 45
degrees gives an unexpected 180 deg.
deg(circ.mean(c(rad(222),rad(45))))%%360 [1] 133.5
deg(circ.mean(c(rad(224),rad(45))))%%360 [1] 134.5
deg(circ.mean(c(rad(225),rad(45))))%%360 [1] 180
[1] 315.5

Can anyone explain this???

This problem was first detected when I was trying to take the circ
weighted means of my data:

With 2 groups of bearings:
x <- c(270,180)
y <- c(45,270)

the circular mean of these bearings gives:
[1] 257.2356

When finding the weighted means I get this:
meany <- circ.mean(rad(y))
meanx <- circ.mean(rad(x))
[1] 281.25

The function for weighted mean I am using:

circ.weighted.mean <- function (x,w)
sinr <- sum(w*sin(x))
cosr <- sum(w*cos(x))
circmean <- atan(sinr, cosr)

I am assuming that the problem that mention above is the cause of the
different mean bearings.

Am I missing something fundamental here?


R-help at mailing list
PLEASE do read the posting guide!

Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 2 of 2 | next ›
Discussion Overview
groupr-help @
postedJun 28, '05 at 5:34p
activeJun 28, '05 at 7:36p

2 users in discussion

Michael Peckford: 1 post Ravi Varadhan: 1 post



site design / logo © 2018 Grokbase