Commit b91955c9 authored by Todd Hayden's avatar Todd Hayden

fixed data.table to data.frame bug

parent e2c26f2a
......@@ -132,13 +132,10 @@
interpolate_path <- function(detections, trans = NULL, int_time_stamp = 86400,
lnl_thresh = 0.9){
# this function uses data.table extensively
setDT(detections)
# make copy of detections for function
dtc <- copy(detections)
detections <- as.data.frame(detections)
dtc <- data.frame(detections)
setDT(dtc)
# subset only columns for function:
dtc <- dtc[, c("animal_id", "detection_timestamp_utc", "deploy_lat", "deploy_long")]
......@@ -152,8 +149,8 @@ interpolate_path <- function(detections, trans = NULL, int_time_stamp = 86400,
setkey(dtc, animal_id, detection_timestamp_utc)
# save original dataset to combine with interpolated data in the end
det <- dtc
names(det) <- c("animal_id", "bin_stamp", "i_lat", "i_lon", "record_type", "num_rows")
det <- copy(dtc)
setnames(det, c("animal_id", "bin_stamp", "i_lat", "i_lon", "record_type", "num_rows"))
# remove any fish with only one detection
dtc <- dtc[num_rows != 1]
......@@ -162,7 +159,8 @@ interpolate_path <- function(detections, trans = NULL, int_time_stamp = 86400,
if (nrow(dtc) == 0) {stop("must have two observations to interpolate")
}
t_seq <- seq(min(dtc$detection_timestamp_utc), max(dtc$detection_timestamp_utc), int_time_stamp)
t_seq <- seq(min(dtc$detection_timestamp_utc),
max(dtc$detection_timestamp_utc), int_time_stamp)
# bin data by time interval and add bin to dtc
dtc[, bin := t_seq[findInterval(detection_timestamp_utc, t_seq)] ]
......@@ -186,8 +184,8 @@ interpolate_path <- function(detections, trans = NULL, int_time_stamp = 86400,
out <- rbind(dtc, det)
setkey(out, animal_id, bin_stamp)
out[, bin_stamp := t_seq[findInterval(bin_stamp, t_seq)] ]
out <- na.omit(out, cols = "i_lat")
names(out) <- c("animal_id", "bin_timestamp", "latitude", "longitude", "record_type")
out <- na.omit(out, cols = "i_lat")
setnames(out, c("animal_id", "bin_timestamp", "latitude", "longitude", "record_type"))
out <- unique(out)
return(as.data.frame(out))
stop
......@@ -392,7 +390,7 @@ interpolate_path <- function(detections, trans = NULL, int_time_stamp = 86400,
out[, !c("animal_id")]
setkey(out, animal_id, bin_stamp)
out[, bin_stamp := t_seq[findInterval(bin_stamp, t_seq)] ]
names(out) <- c("animal_id", "bin_timestamp", "latitude", "longitude", "record_type")
setnames(out, c("animal_id", "bin_timestamp", "latitude", "longitude", "record_type"))
out <- na.omit(out, cols = "latitude")
out <- unique(out)
return(as.data.frame(out))
......
......@@ -71,14 +71,7 @@
#'
#' # take a look
#' head(dtc)
#'
#' setDT(dtc)
#'setkey(dtc, animal_id)
#'dtc <- dtc[.(c("153", "22", "23"))]
#'
#'write.csv(dtc, "test.csv")
#'
#'
#'
#' # load receiver location data
#' rec_file <- system.file("extdata",
#' "receiver_locations_2011.csv", package = "glatos")
......@@ -154,7 +147,7 @@ make_frames <- function(proc_obj, recs = NULL, out_dir = getwd(),
}
# Convert proc_obj and recs dataframes into data.table objects
work_proc_obj <- proc_obj
work_proc_obj <- data.frame(proc_obj)
setDT(work_proc_obj)
......@@ -198,10 +191,10 @@ make_frames <- function(proc_obj, recs = NULL, out_dir = getwd(),
# merge by overlap
work_proc_obj <- foverlaps(work_proc_obj, dur, type = "within",
nomatch = 0L, by.x = c("bin_timestamp", "t_end"))
work_proc_obj <- work_proc_obj[, c("animal_id", "t_seq", "latitude", "longitude",
"record_type")]
names(work_proc_obj) <- c("animal_id", "bin_timestamp", "latitude", "longitude",
"record_type")
work_proc_obj <- work_proc_obj[, c("animal_id", "t_seq", "latitude",
"longitude", "record_type")]
setnames(work_proc_obj, c("animal_id", "bin_timestamp", "latitude",
"longitude", "record_type"))
work_proc_obj[, grp := bin_timestamp]
}
......
......@@ -81,7 +81,7 @@ dtc <- read_glatos_detections(det_file)
# take a look
head(dtc)
# load receiver location data
rec_file <- system.file("extdata",
"receiver_locations_2011.csv", package = "glatos")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment