Greg
Greg

Reputation: 3660

determine if contents of a vector are mostly positive or negative

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 +/- 1200s and +/- 100s), 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

Answers (1)

r2evans
r2evans

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

Related Questions