Skip to content

Instantly share code, notes, and snippets.

@bniebuhr
Last active April 19, 2022 08:38
Show Gist options
  • Save bniebuhr/f040a2f352150b1a8239ff2ec258468b to your computer and use it in GitHub Desktop.
Save bniebuhr/f040a2f352150b1a8239ff2ec258468b to your computer and use it in GitHub Desktop.
Read table and dates in different formats - for Leonie
library(dplyr)
library(tibble)
# read table
tab <- read.csv("results/calving_period_bn.csv")
# this is not necessary but I find it easier to read data.frames as tibbles
tab <- tibble::as_tibble(tab)
tab <- dplyr::rename(tab, rowid = X)
tab
# set dates
tab$date_begin <- as.POSIXct(tab$date1, format = "%d/%m/%Y %H:%M", tz = "UTC")
rows_diff_format <- 91:nrow(tab)
tab$date_begin[rows_diff_format] <- as.POSIXct(as.numeric(tab$date1[rows_diff_format]),
origin = "1970-01-01", tz = "UTC")
tab$date_end <- as.POSIXct(tab$date2, format = "%d/%m/%Y %H:%M", tz = "UTC")
tab$date_end[rows_diff_format] <- as.POSIXct(as.numeric(tab$date2[rows_diff_format]),
origin = "1970-01-01", tz = "UTC")
# check end dates are always after begin dates
tab$date_end > tab$date_begin
which(!(tab$date_end > tab$date_begin))
dplyr::slice(tab, 198) # issue in line 198
# remove dates with wrong format
tab <- dplyr::select(tab, -c(date1, date2))
# sort by year and id
tab <- dplyr::arrange(tab, year, id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment