Using suncalc to Create a Data Table with Sunrise and Sunset Times for Each Date with Individual Geographic Locations
As a data analyst or scientist, it’s common to work with large datasets that require additional context, such as sunrise and sunset times. In this response, we’ll explore how to use the suncalc package in R to create a data table with sunrise and sunset times for each date with individual geographic locations.
Background
The suncalc package is a part of the tidyverse suite of packages that provides functions for calculating solar time. It uses data from the National Oceanic and Atmospheric Administration (NOAA) and the United States Naval Observatory to provide accurate sunrise and sunset times for any given location and date.
To use suncalc, you’ll need to install the package using install.packages("suncalc"). You can also load the package directly into your R session using library(suncalc).
Prerequisites
Before we begin, make sure you have the following packages installed:
suncalclubridate(used for date manipulation)tidyverse(includesdplyr,tidyr, andstringr)
You can install these packages using the following commands:
install.packages("suncalc")
install.packages("lubridate")
install.packages("tidyverse")
Step 1: Load Necessary Packages
First, load the necessary packages into your R session.
library(suncalc)
library(lubridate)
library(tidyverse)
Step 2: Create a Data Frame with County Information
Create a data frame with county information using data.frame().
my_df <- data.frame(
County = c("Grand Junction", "Larimer"),
Latitude = c(65,62),
Longitude = c(-80,-75)
)
This will create a simple data frame with three columns: County, Latitude, and Longitude.
Step 3: Generate Dates for the Year
Use seq() from the lubridate package to generate dates for the year.
start.date <- "20200101"
end.date <- "20201231"
Dates <- seq(ymd(start.date), ymd(end.date), by = "days")
This will create a sequence of dates from January 1, 2020, to December 31, 2020.
Step 4: Create a Data Frame with Sunrise and Sunset Times
Use expand.grid() from the tidyverse package to create a data frame with all possible combinations of dates and counties.
sun_df <- expand.grid(Dates = Dates, County = my_df$County)
This will create a data frame with two columns: Dates and County.
Next, use left_join() from the tidyverse package to join this data frame with the original my_df data frame on both counties.
sun_df <- sun_df %>%
left_join(my_df) %>%
group_by(Dates, County, Latitude, Longitude)
This will create a data frame with all three columns from my_df: County, Latitude, and Longitude.
Step 5: Calculate Sunrise and Sunset Times
Use the getSunlightTimes() function from the suncalc package to calculate sunrise and sunset times for each date and location.
sun_df <- sun_df %>%
mutate(sunrise = getSunlightTimes(Dates, Latitude, Longitude, tz = "MST")$sunrise,
sunset = getSunlightTimes(Dates, Latitude, Longitude, tz = "MST")$sunset)
This will create a new data frame with three additional columns: sunrise, sunset, and the original Dates and County columns.
Example Output
Here’s an example of what the output might look like:
> head(sun_df)
# A tibble: 6 × 6
# Groups: Dates, County, Latitude, Longitude [6]
Dates County Latitude Longitude sunrise
<date> <chr> <dbl> <dbl> <dttm>
1 2020-01-01 Grand Junction 65 -80 2020-01-01 08:28:20
2 2020-01-02 Grand Junction 65 -80 2020-01-02 08:27:05
3 2020-01-03 Grand Junction 65 -80 2020-01-03 08:25:41
4 2020-01-04 Grand Junction 65 -80 2020-01-04 08:24:10
5 2020-01-05 Grand Junction 65 -80 2020-01-05 08:22:32
6 2020-01-06 Grand Junction 65 -80 2020-01-06 08:20:46
This output shows the sunrise and sunset times for each date and location.
Conclusion
In this response, we’ve explored how to use the suncalc package in R to create a data table with sunrise and sunset times for each date with individual geographic locations. We’ve walked through the steps required to load necessary packages, create a data frame with county information, generate dates for the year, create a data frame with sunrise and sunset times, and calculate sunrise and sunset times using getSunlightTimes(). With this tutorial, you should be able to use suncalc to create similar output for your own projects.
Last modified on 2025-04-21