Message Boards Message Boards

0
|
4198 Views
|
5 Replies
|
2 Total Likes
View groups...
Share
Share this post:

How to get Count[] to count positive integers?

Posted 3 years ago

Try as I may, I can not get Count[] to count positive integers. My data

change = FinancialData["UPST", 
  "Change", {DatePlus[Today, {-70, "BusinessDay"}], {2021, 10}}]

Try to get a pure List

cc = change[[2, 1]] // Normal // Flatten

Any thing I do, the result is 0, can not get a true response

Count[#, # == Positive] & /@ cc // QuantityMagnitude
POSTED BY: Raymond Low
5 Replies
Posted 3 years ago
Count[cc // QuantityMagnitude,  u_ /; u < 0]

So cc//QuantityMagnitude is the first argument to Count and that first argument should get evaluated resulting in a list of numbers. And u_/;u<0 is a pattern that is going to get used on each of those items in that list. And Count will total up the number where it matched.

If we keep at this then one or the other of us will trip over something and figure this out.

POSTED BY: Bill Nelson
Posted 3 years ago

Another option

CountsBy[cc, Positive]
(* <|True -> 33, False -> 36|> *)

The result is different because of the {-70, "BusinessDay"}

POSTED BY: Rohit Namjoshi
Posted 3 years ago

Other combinations I have tried without success

Count[cc, Positive]
Count[cc, "-"]
Count[#, Positive] & /@ cc // QuantityMagnitude
Count[#, "-" ] & /@ cc // QuantityMagnitude
Count[#, u_ /; u_ == Positive] & /@ cc // QuantityMagnitude
Count[#, Positive[#]] & /@ cc
Count[Positive[#]] & /@ cc   (* as least the function Positive[] \
works*)
Count[Positive[#], True] & /@ cc   
Length@Select[Positive[#]] & /@ cc // QuantityMagnitude

I used QuantityMagnitude[] to strip off the units ($) cc looks like this

In[416]:= cc // QuantityMagnitude

Out[416]= {-20.915, -0.814995, -1.66, -4.02, 7.3, -2.45, -2.51, -1.7, \
5.49, -1.72, -1.22, 1.69, -0.959999, 1.97, -0.980003, -2.71, -1.21, \
-2.6, 2.35, 0.25, 9.89999, -10.52, -6.25, -0.0400009, 0.700005, 1.49, \
2.35, 1.52, -2.24, -0.340004, 5.25, -6.07, 6.04, 2.08, -3.11, 12.65, \
-4.73001, -0.699989, 5.18999, -1.03999, 5.06, -1.51001, 35.52, 6.92, \
25.17, -2.39999, -0.710007, 12.09, -18.4, 1.77, 7.32001, 16.49, \
-5.31, 2.61, 6.42999, -7.53999, 13.48, -2.31, 12.99, 7.48999, 15.41, \
11.63, 15.27, -19.14, 7.54001, -8.95999, 10.05, 14.17, 10.06, \
-9.85001}

cc is just a list of positive and negative numbers

POSTED BY: Raymond Low
Posted 3 years ago

Thanks for helping Bill

I tried the () on all the combinations I tried before and the one you suggested but Count[] or Length[] does not give a count. The inner code I get all zeros, or all ones, or all twos or just plain 0.

Count[cc, Positive]
Count[cc, "-"]
Count[#, Positive] & /@ (cc // QuantityMagnitude)
Count[#, "-" ] & /@ (cc // QuantityMagnitude)
Count[#, u_ /; u_ == Positive] & /@ (cc // QuantityMagnitude)
Count[#, Positive[#]] & /@ cc
Count[Positive[#]] & /@ cc   (* as least the function Positive[] \
works*)
Count[Positive[#], True] & /@ cc   
Length@Select[Positive[#]] & /@ (cc // QuantityMagnitude)
POSTED BY: Raymond Low
Posted 3 years ago

I did, or I thought I did, but I have learned that if you go through the motions, what you did, it is not always what you thought you did and this is another one of those cases. I thought I copied your code

In[558]:= Count[#, u /; u < 0] & /@ (cc // QuantityMagnitude)

Out[558]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0}

so I checked again, and what I thought I did, I did not do, So yes, copying your code correctly gives me a correct result. Thank you very much, I am sorry for the mistake.

In[559]:= Count[cc // QuantityMagnitude, u_ /; u < 0]

Out[559]= 34
POSTED BY: Raymond Low
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract