Skip to content

Commit

Permalink
Change options() to keep millisecond information. Copy raw data and c…
Browse files Browse the repository at this point in the history
…hange by reference.
  • Loading branch information
mhpob committed Jul 7, 2021
1 parent a73551e commit 464e3f2
Showing 1 changed file with 42 additions and 9 deletions.
51 changes: 42 additions & 9 deletions R/prepFiles.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,49 @@
#' prepped_detections <- prepDetections("path-to-raw-data-file", type="vemco_vue")
#' }
prepDetections <- function(raw_dat, type){
detections <- data.table::data.table()

# gsub (and the previous version using strsplit) convert to character before
# doing their thing. The default number of millisecond digits for R to
# display is 0, so millisecond information is dropped when converted to
# character. Temporarily change this to keep milliseconds, then used on.exit
# to change the option back to what it was before when the function finishes
op <- options(digits.secs = 3)
on.exit(options(op), add = T)

detections <- data.table::copy(raw_dat)

if (type == "vemco_vue"){
detections[, ts := as.POSIXct(raw_dat$'Date and Time (UTC)', tz = "UTC")]
detections[, tag := as.numeric(gsub('.*-', '', raw_dat$Transmitter))]
detections[, epo := as.numeric(ts)]
detections[, frac := as.numeric(gsub('.*\\.', '',
raw_dat$"Date and Time (UTC)")) / 1000]
detections[, serial := as.numeric(gsub('.*-', '', raw_dat$Receiver))]

# Only parse datetime if needed
## trunc drops milliseconds but converts to POSIXlt in the process, so it needs
## to be converted back to POSIXct for use elsewhere.

if(!inherits(detections$`Date and Time (UTC)`, 'POSIXt')){

detections[, ts :=
as.POSIXct(
trunc(
as.POSIXct(`Date and Time (UTC)`, tz = "UTC")
)
)]

} else{

detections[, ts :=
as.POSIXct(
trunc(`Date and Time (UTC)`)
)]

}

detections[, ':='(tag = as.numeric(gsub('.*-', '', Transmitter)),
epo = as.numeric(ts),
frac = as.numeric(gsub('.*\\.', '',
`Date and Time (UTC)`)) / 1000,
serial = as.numeric(gsub('.*-', '', Receiver)))]
}
detections[]
return(detections)

detections[, .(ts, tag, epo, frac, serial)]

}

0 comments on commit 464e3f2

Please sign in to comment.