0
votes

I've got GPS data from different herbivore species. This gives me multiple datapoints per day with date, time, longitude, latitude, etc. I am trying to sort this data into day and night, using the sunrise and sunset times. I tried doing this with sunrise.set from the package StreamMetabolism. The problem I get however, is that it assumes that every row belongs to a new date. Does anyone know how I can link the date in this function to the date column in my dataset?

This is what I've tried. num.days is set to 27956, as that's the number of rows in my dataset. newgpsdata$sunRise <- sunrise.set(51.700998, 5.626467, "2016/03/08", timezone = "Europe/Amsterdam", num.days = 27956)

newgpsdata$sunRise <- sunrise.set(51.700998, 5.626467, newgpsdata$Date, timezone = "Europe/Amsterdam") This returns the error "Error in seq.POSIXt(from = day, length.out = num.days, by = "days") : 'from' must be of length 1"

1

1 Answers

0
votes

The tricky bit is to feed the dates piece by piece to sunrise set. That's possible with base R but installing {dplyr} or the tidyverse collection of packages makes the code more legible. Using {dplyr} and chaining commands with the pipe operator %>%:

d <- data.frame(Date = c("2021/10/15", "2021/11/04", "2022/01/10"))

d %>%
    rowwise %>%
    mutate(
        daylight = sunrise.set(51.700998, 5.626467, Date, num.days = 1)
        sunrise = daylight$sunrise,
        sunset = daylight$sunset
    )

note that sunrise.set requires dates to be given as yyyy/mm/dd (package {lubridate} helps a lot)