Added the small utility function hms2seconds()

Converts character strings of hours, minutes, seconds to seconds (as numeric).
master
Taha Ahmed 13 years ago
parent 7d6044143b
commit 1a52116071

@ -27,7 +27,7 @@ SubstrateHistory <- function(sampleid) {
empty.names <- names(sample.history)[which(empty.cols)]
# Remove the identified empty columns plus those columns deemed unwanted
sample.history <- sample.history[, -c(which(empty.cols == TRUE),
which(names(sample.history) == "sampleid"),
#which(names(sample.history) == "sampleid"),
which(names(sample.history) == "created"),
which(names(sample.history) == "project"))]
# Save the empty column names as attribute to sample.history dataframe

@ -0,0 +1,28 @@
##################################################
################# hms2seconds ####################
##################################################
hms2seconds <- function(hms_vec) {
## Description:
## Converts an hh:mm:ss time into seconds.
## Usage:
## hms2seconds(hh:mm:ss)
## Arguments:
## hms_vec: a character vector
## Value:
## A numeric vector with the same number of elements
## as the input vector
#
seconds <- rep(NA, length(hms_vec))
for (i in 1:length(hms_vec)) {
hms_str <- strsplit(hms_vec[i], ":")[[1]]
# We assume hours:min:sec, anything else, throw an error
if (length(hms_str) != 3) {
error("Input must be formatted as hh:mm:ss")
}
seconds[i] <-
as.numeric(hms_str[1]) * 3600 +
as.numeric(hms_str[2]) * 60 +
as.numeric(hms_str[3])
}
return(seconds)
}
Loading…
Cancel
Save