Skip to content

Instantly share code, notes, and snippets.

View Myfanwy's full-sized avatar

Myfanwy Johnston Myfanwy

View GitHub Profile
@Myfanwy
Myfanwy / about_time.R
Last active August 10, 2023 21:06
Datetime gotchas in R
# About time in R
# M. Johnston
# Thu Aug 10 13:06:35 2023 America/Los_Angeles ------------------------------
#---------------------------------------------------------#
# Takeaway: For date-times in R (POSIXt objects), there are differences between what
# your R session references, stores, and displays. It's very important to be aware
# of these differences to avoid date time gotchas.
#---------------------------------------------------------#
@Myfanwy
Myfanwy / DWR_training_2021-01-20
Last active January 17, 2021 19:37
Preps your system for the things we'll be doing in the training session on Wed
install.packages(c('rmarkdown',
'knitr',
'lubridate',
'ggplot2',
'data.table',
'readxl',
'RSQLite',
'dplyr'
)
)
library(tidyverse)
# Previous version of SBM used 120 mm cutoff for rearing
# Exploring idea of using hypothetical continuous relationship between rearing probability and fork length
## Four-parameter logistic model gives nice control over the curve
# a = max value
# b = steepness of the curve
# c = inflection point
# d = min value
@Myfanwy
Myfanwy / stantec_2020-03-03_demo.R
Last active March 17, 2020 18:06
Stantec Demo code
#--------------------------------------------#
# ARTEMIS Demo
# Stantec presentation, 2020-03-03
# M. Johnston, Cramer Fish Sciences
# This script presents an introduction to data simulation in {artemis}.
#--------------------------------------------#
# Goal: Simlulate data and visualize relationships
#-------------------------------------------------------#
@Myfanwy
Myfanwy / parse_tag_info.r
Last active August 14, 2019 20:30
parse_tag_info.r
# separates the vemco column formatting of freq-codespace-tagid, then returns the original dataframe with the codespace and tagids as separate columns
parse_tag_info <- function(df, tagcol = "TagID") {
names(df)[names(df) == tagcol] <- "SepTagID" # rename the old combined tagid col
out <- as.data.frame(do.call(rbind, strsplit(as.character(df$SepTagID),'-')),
stringsAsFactors = FALSE)
colnames(out) <- c("freq", "CodeSpace", "TagID")
final <- cbind(df, out)
drops <- c("freq", "SepTagID")
@Myfanwy
Myfanwy / dc_template_instructions
Created July 12, 2019 21:57
How to drift-correct or time-correct a .csv file from a VUE .vdb
If the original vrl files are unavailable, a drift-correction needs to be performed on the .csv file for each receiver in a deployment. You will need at least a .vdb with the receiver log of interest to be able to do this.
To do this, access the events tab in VUE for a receiver download. Export it to a csv.
Open it in Excel. Make sure the PC Time (very last cell in the Description column) is in an acceptable date/time format. Do this by copying and pasting it to the cell below it, and using the custom m/d/yyyy hh:mm:ss.00 format.
Events Tab:
In a new column, calculate the time between initialization and end time (days). Do this by subtracting A1 from the final cell in the A column. This is done in J2 in the example
Covert J2 to seconds (multiply by 84600). This is done in K2.
@Myfanwy
Myfanwy / tides.R
Last active January 3, 2019 22:02
Tide forecasting
#Goal: identify fieldwork periods by finding high tide on for each of the next 2 weeks.
# There are two high tides per day. A high high tide and a lower high tide. Adding ~25.3 hours to each one, respectively, gives you the # date/times of the next ones.
# So for the two high tides on 1/1/19:
tides <- structure(c(1546341300, 1546384500), class = c("POSIXct", "POSIXt"
), tzone = "Pacific/Pitcairn")
# adding 25.3 hours to this vector gives us the date/times of the next two high tides:
tides + as.difftime(25.3, units = "hours").
@Myfanwy
Myfanwy / timer.R
Created July 28, 2017 04:31
Timer function for R
# Depends on the `beepr` package by @rasmusab: https://cran.r-project.org/web/packages/beepr/README.html
# Code greatly improved by flodel on Stack Exchange Code Review (thanks flodel)
# Planned improvements/expansions: new end-of-timer sounds, add functionality for starting a separate R session so that it doesn't bogart the current one.
timer <- function(interval, units = c("secs", "mins", "hours", "days", "weeks")) {
units <- match.arg(units)
num_sec <- interval * switch(units, secs = 1, mins = 60, hours = 3600,
days = 86400, weeks = 604800)
Sys.sleep(num_sec)
if (require(beepr)) beep(2) else message("TIMER DONE YO!")
tt %>%
mutate(
laststation = as.character(laststation),
TagID = as.character(TagID)
) %>%
group_by(laststation) %>%
ggplot(aes(TagID, kmday)) +
geom_segment(aes(xend=TagID, yend=0)) +
geom_point(shape="—", size=5) +
facet_wrap(~laststation, nrow=1, scales="free_x") +