Skip to content

Commit

Permalink
rmin and rmax
Browse files Browse the repository at this point in the history
  • Loading branch information
shanedrabing committed Jul 21, 2021
1 parent 1e69352 commit 38ed832
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
24 changes: 24 additions & 0 deletions pyrat/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
"unique",
"vector",
"which",
"rmin",
"rmax",
]


Expand Down Expand Up @@ -132,6 +134,28 @@ def sqrt(x):
return x.apply(catch(math.sqrt, TypeError, NA))


def rmin(*x, na_rm=False):
x = c(x)
if not x:
return Inf
if na_rm:
x = x[~is_na(x)]
elif any(is_na(x)):
return NA
return min(x)


def rmax(*x, na_rm=False):
x = c(x)
if not x:
return -Inf
if na_rm:
x = x[~is_na(x)]
elif any(is_na(x)):
return NA
return max(x)


def mean(x, na_rm=False):
if not x:
return NA
Expand Down
11 changes: 11 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,14 @@ def none(x):
assert_identical(mean(c(1, 2)), 1.5)
assert_identical(mean(c(1, 2, NA)), NA)
assert_identical(mean(c(1, 2, NA), na_rm=True), 1.5)

# rmin
assert_eq(rmin(), Inf)
assert_eq(rmin(1, c(NA, 2)), NA)
assert_eq(rmin(1, NA, 2, na_rm=True), 1)

# rmax
assert_eq(rmax(), -Inf)
assert_eq(rmax(1, c(NA, 2)), NA)
assert_eq(rmax(1, NA, 2, na_rm=True), 2)

0 comments on commit 38ed832

Please sign in to comment.