Question

I need to create a function for some work I'm doing on directional statistics. I want to show the distribution of flood events using a circle and calculate the mean direction and variance.

I need to calculate the angular value in radians by multiplying the julian date by (360/365). I am having problems because I need a function that takes account of the leap years in the 40 year record I am considering. i.e. IF leap year angular value = julian date x (360/366).

The data I am using is Peaks above threshold so I do not have a piece of data for every year and in some years I have more than one entry

Date            Time    Flow
04/05/1973  00:00   44.67
22/06/1974  00:00   128.38
22/11/1974  23:45   129.15
26/09/1976  22:00   89.51
15/10/1976  00:00   139.35
24/02/1978  19:30   183.69
27/12/1978  04:00   229.65
18/03/1980  09:15   117.7
02/03/1981  22:00   262.39

Many thanks Rich

Was it helpful?

Solution

There may be a more elegant way to do this, but try

df$Year<-format(df$Date,"%Y")

that should put just the year if a single column. Then make a new column to indicate if it is a leap year

df$Leap<-0
df$Leap[df$Year=="1972" | df$ Year=="1976" |df$Year=="1980"]<-1

depending on your data, you may find it easier to change to a number and then use the %% to see if you can divide it evenly by 4, but beware of the year 2000.

Then you can use an if statement to the effect of

if (df$Leap==0) {do * 360/365} else {do * 360/366}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top