Skip to content

Commit 8796ab8

Browse files
authored
Merge pull request #135 from dajmcdon/km-fix-middlel-middler
Resolved issue #127 and added tests for even-lengthed vectors as well.
2 parents e3eeb39 + 3de9e8c commit 8796ab8

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Diff for: R/utils.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Median = function(x) median(x, na.rm = TRUE)
2323

2424
Start = function(x) x[1]
2525
End = function(x) x[length(x)]
26-
MiddleL = function(x) x[floor(length(x)/2)]
27-
MiddleR = function(x) x[ceiling(length(x)/2)]
26+
MiddleL = function(x) x[floor((length(x)+1)/2)]
27+
MiddleR = function(x) x[ceiling((length(x)+1)/2)]
2828
ExtendL = function(x) c(Start(x), x)
2929
ExtendR = function(x) c(x, End(x))
3030

Diff for: tests/testthat/test-utils.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ test_that("Other capital letter functions work",{
2626
x <- c(1,2,3,4,5)
2727
expect_equal(Start(x),1)
2828
expect_equal(End(x),5)
29-
expect_equal(MiddleL(x),2) # Questionable for odd length vectors
30-
expect_equal(MiddleR(x),3) # Questionable for odd length vectors
29+
expect_equal(MiddleL(x),3)
30+
expect_equal(MiddleR(x),3)
31+
expect_equal(MiddleL(x[-5]),2)
32+
expect_equal(MiddleR(x[-5]),3)
3133
expect_equal(ExtendL(x),c(1,1,2,3,4,5))
3234
expect_equal(ExtendR(x),c(1,2,3,4,5,5))
3335
})

0 commit comments

Comments
 (0)