I have a data set like this over 23 days:
temp1 DateTime 1 448 2016-03-24 21:14:00 2 448 2016-03-24 21:14:01 3 448 2016-03-24 21:14:02 4 448 2016-03-24 21:14:03 5 448 2016-03-24 21:14:04 6 448 2016-03-24 21:14:05 1930724 500 2016-04-15 13:46:21 1930725 500 2016-04-15 13:46:22 1930726 500 2016-04-15 13:46:23 1930727 500 2016-04-15 13:46:24 1930728 500 2016-04-15 13:46:25 1930729 500 2016-04-15 13:46:26
I now want to add a new column, that writes day
for every DateTime
- value greater than the sunrise time
and smaller than the sunset time
, so I get:
temp1 DateTime dayNight 1 448 2016-03-24 21:14:00 night 2 448 2016-03-24 21:14:01 night 3 448 2016-03-24 21:14:02 night 4 448 2016-03-24 21:14:03 night 5 448 2016-03-24 21:14:04 night 6 448 2016-03-24 21:14:05 night 1930724 500 2016-04-15 13:46:21 day 1930725 500 2016-04-15 13:46:22 day 1930726 500 2016-04-15 13:46:23 day 1930727 500 2016-04-15 13:46:24 day 1930728 500 2016-04-15 13:46:25 day 1930729 500 2016-04-15 13:46:26 day
I get the times for sunrise and sunset like "2016-04-15 06:40:37 UTC"
from the functions:
sunRise <- function(x){ sunrise.set(lat, long, x, timezone = "UTC", num.days = 1)[1,1] }
and
sunSet <- function(x){ sunrise.set(lat, long, x, timezone = "UTC", num.days = 1)[1,2] }
with the sunrise.set()
- function from the package StreamMetabolism
, with lat <- 28.39675
and long <- -16.567131
Now the issue is that those sun times change over the 23 days of my data frame.
I there a way to make my dayNight
column, based on the specific sunrise/-set times for every single day?
And is there a way to add the sunrise/-set times of every day as a column to the data frame?
Here is one approach (based on my comment) and made up data I could come up with:
library(dplyr) # You don't need this line if you already have DateTime in proper format df$DateTime <- as.POSIXct(df$DateTime) # Add a date column (with whatever timezone you want) df$date <- as.Date(df$DateTime, tz = 'EST') # Following generates the sunrise and sunset times for the two example dates sunRise <- c(as.POSIXct('2016-04-15 06:40:37'), as.POSIXct('2016-03-24 06:55:00')) sunSet <- c(as.POSIXct('2016-04-15 18:40:37'), as.POSIXct('2016-03-24 18:25:00')) sun <- data.frame(date = as.Date(sunRise, tz = 'EST'), sunRise = sunRise, sunSet = sunSet) sun date sunRise sunSet 1 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37 2 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00 # Join the two tables and compute night/day df <- inner_join(df, sun) df$dayNight <- ifelse(df$DateTime > df$sunRise & df$DateTime < df$sunSet, 'day', 'night')
Output is as follows:
df temp1 DateTime date sunRise sunSet dayNight 1 448 2016-03-24 21:14:00 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00 night 2 448 2016-03-24 21:14:01 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00 night 3 448 2016-03-24 21:14:02 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00 night 4 448 2016-03-24 21:14:03 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00 night 5 448 2016-03-24 21:14:04 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00 night 6 448 2016-03-24 21:14:05 2016-03-24 2016-03-24 06:55:00 2016-03-24 18:25:00 night 7 500 2016-04-15 13:46:21 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37 day 8 500 2016-04-15 13:46:22 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37 day 9 500 2016-04-15 13:46:23 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37 day 10 500 2016-04-15 13:46:24 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37 day 11 500 2016-04-15 13:46:25 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37 day 12 500 2016-04-15 13:46:26 2016-04-15 2016-04-15 06:40:37 2016-04-15 18:40:37 day
Of course, you can remove unnecessary columns and retain what you want as follows:
df[, -c(3, 4, 5)] temp1 DateTime dayNight 1 448 2016-03-24 21:14:00 night 2 448 2016-03-24 21:14:01 night 3 448 2016-03-24 21:14:02 night 4 448 2016-03-24 21:14:03 night 5 448 2016-03-24 21:14:04 night 6 448 2016-03-24 21:14:05 night 7 500 2016-04-15 13:46:21 day 8 500 2016-04-15 13:46:22 day 9 500 2016-04-15 13:46:23 day 10 500 2016-04-15 13:46:24 day 11 500 2016-04-15 13:46:25 day 12 500 2016-04-15 13:46:26 day