Day and night

Create a column with the classification day or night

Miriam Lerma true
2022-12-04

Intro

This post is to create a column day or night.

Data

For the exercises, test data is from masked boobies.
To access the data you have to install the package sula: devtools::install_github(“MiriamLL/sula”)

#devtools::install_github("MiriamLL/sula")
library(sula)
GPS01<-(GPS_01)

Transform time and date

Identify column with time date data

GPS01$dt <- as.POSIXct(strptime(GPS01$tStamp, "%Y-%m-%d %H:%M:%S"))

Extract hours

Once is in time and date format, you can extract just the hours

GPS01$hour <- as.numeric(substr(GPS01$dt, 12, 13))

Classify day and night

Create a classification base on the time of sunset and sunrise.

GPS01<-GPS01 %>%
 mutate(
    day_night = case_when(
      hour > 20 |  hour < 6 ~ "night",
      TRUE  ~ "day"
    ))

Quantify proportions

Now you can quantify how much your animals were out at night

GPS01 %>%
  group_by(day_night)%>%
  count()%>%
  pivot_wider(names_from = day_night, values_from = n)
# A tibble: 1 × 2
    day night
  <int> <int>
1   645   393

I hope this might help you

Further reads

Classify activities at night in penguins