Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update numeric_time #43

Merged
merged 3 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ Suggests:
LazyData: TRUE
License: GPL (>=2)
VignetteBuilder: knitr
RoxygenNote: 7.2.1
RoxygenNote: 7.3.1
Encoding: UTF-8
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# mrgmisc development

## Bug fixes

- `numeric_time` was udpated to work with negative times. (#43)

# mrgmisc 0.1.5

## New features and changes
Expand Down
9 changes: 7 additions & 2 deletions R/numeric_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ numeric_time <- function(.col) {

sapply(strsplit(.col,":"),
function(x) {
x <- as.numeric(x)
round(x[1]+x[2]/60,2)
x_num <- as.numeric(x)
x_num <- abs(x_num)
res <- round(x_num[1]+x_num[2]/60,2)
if (grepl("-", x[1])) {
res <- res * -1
}
res
}
)
}
31 changes: 31 additions & 0 deletions man/mrgmisc-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-numeric_time.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,12 @@ test_that("numeric_time accurately calculates the decimal time from type POSIXct
expect_equal(numeric_time(times)[1], 12.57)
expect_equal(numeric_time(times)[2], 4.53)
})

test_that("numeric_time handles negative times correctly", {

times2 <- c("3:05", "-3:05", "-11:40")
numeric_time(times2)

expect_equal(numeric_time(times2)[1], numeric_time(times2)[2]*-1)

})