Reputation: 3660
I want to test whether a vector contains mostly positive numbers, mostly negative numbers, or an even mix.
Here are three example vectors. They're based on real data, which can have outliers in either direction (the +/- 1200
s and +/- 100
s), so just summing or averaging the vectors won't work.
z_negative <- c(seq(1, -10, -1), 1200, -100, seq(-10, 1, 1))
z_positive <- c(1, 5, 3, 4, -1200, 100, 8, 9, 11, 5, 10, 8, -1 , 5, 3, 0, -1)
z_tie <- c(1, 1, 1200, 0, 0, 0, -1, -100, -2)
I can use sign
twice, with mean
or sum
and achieve what I'm looking for, but the two sign
functions look strange to me. Is there a better way (ideally in base
)?
sign(mean(sign(z_negative)))
[1] -1
sign(sum(sign(z_positive)))
[1] 1
sign(sum(sign(z_tie)))
[1] 0
Upvotes: 0
Views: 577
Reputation: 160447
You can get the relative counts with table(sign(...))
:
table(sign(z_negative))
# -1 0 1
# 21 2 3
table(sign(z_positive))
# -1 0 1
# 3 1 13
table(sign(z_tie))
# -1 0 1
# 3 3 3
You can quantify if something is more or less positive with the ratio of the "-1"
and "1"
counts (note the quotes ... don't use -1
and 1
):
with(list(x = table(sign(z_positive))), x["1"] / x["-1"])
# 1
# 4.333333
with(list(x = table(sign(z_negative))), x["1"] / x["-1"])
# 1
# 0.1428571
with(list(x = table(sign(z_tie))), x["1"] / x["-1"])
# 1
# 1
Upvotes: 1