0

I have a data frame with n rows each of which corresponds to a single event in space and time. The data frame has columns containing spatial coordinates and the date in Julian days as well as several other columns of additional data.

There are various things I would like to do with my data but as an example I want to rasterise some of the columns and output some maps. For most of my columns I can do this easily with something like this:

df.raster <- rasterize(df.sp, base.raster, field = "column", fun=median) 
plot(df.raster)

However, for Julian days this doesn't make sense because its cyclical. 365/366 is adjacent to 1 but R doesn't know this so using the median function isn't going to provide me with a meaningful number. I'm looking for a way to convert my column of Julian days into a new column which reflects this and enables me to create a raster of meaningful values for Julian day.

My Julian days column runs from 1-366 reflecting the day on which an event took place within a particular year. My data covers multiple years but my Julian days column starts from 1 again at the start of every year.

I've tried a few things including converting to radians but nothing has worked so far. Any help would be much appreciated!

James
  • 1,164
  • 2
  • 15
  • 36
  • If you hover over the "julian-date" tag, you will see the actual definition of Julian date associated with the tag. There are related day numbers that might be easier to work with in some situations, such as [Lillian date](https://en.wikipedia.org/wiki/Lilian_date). Algorithms for converting regular dates into one of these day numbers are widely available. Be cautious of sloppy day # definitions in the R language. – Gerard Ashton Jan 28 '16 at 15:04
  • Yeah I did notice the actual definition in the tag and originally had a bit in the penultimate paragraph stating that my "Julian date" didn't meet that definition. However, using the Julian days tag seemed the most likely way to flag my question to someone who had experience of my problem and could provide an example of how to solve it. @GerardAshton – James Jan 29 '16 at 13:55
  • I'm suggesting the real, official Julian date, or the shorter Lillian date, might be a solution to your problem, because, for example, Dec. 31, 2015 is Lillian date 158227 and Jan. 1, 2016 is Lillian date 158228, so the Lillian dates are numerically adjacent, as you desire. – Gerard Ashton Jan 30 '16 at 00:54
  • Ah thanks! I'll look into that! I want to combine my data so I have (for example) the mean value on January 1st across all 15 years of data I have but this might be a place to start @GerardAshton – James Feb 01 '16 at 12:23

1 Answers1

2

To get what I wanted I first had to scale my "Julian days" column to degrees, then I could convert degrees to radians using the as_radians function in the aspace package and then I could use circular statistics on the radians:

# Scale Julian days to degrees

df$degrees <- (df$jday/366)*360

# Convert degrees to radians

df$radians <- as_radians(df$degrees)

# Convert df to a spatial object

df.sp <- df
coordinates(df.sp) <- ~ x + y
proj4string(df.sp) <- proj4string(coordinates)

# Rasterise radians

radians.raster <- rasterize(df.sp, base.raster, field = "radians", fun = mean.circular)

# Plot rasterised radians

plot(radians.raster)

Currently the figures will be slightly inaccurate as (when converting to degrees) leap years should be divided by 366 and non-leap years by 365 but I'll fix this with a simple loop which looks up the year (also included in my df) for each row and uses 366/365 appropriately.

James
  • 1,164
  • 2
  • 15
  • 36