3
$\begingroup$

Consider this data set:

BNikkei = {{10.0032, 27 Days}, {10.0527, 22 Days}, {10.1283, 18 Days}}

from a recent question, "More effective way to apply a function in one member of a pair .."

How to get Select[ to work for #[[2]] < 22 Days? I tried:

Select[BNikkei, #[[2]] < (22 Days) &]
(* {} *)

Units in MMA are a great convention. I can't seem to find too much in manuals, on this situation. Unless, I have to derive comparison functions, for my own types??? Thanks in advance.

$\endgroup$
1
  • 2
    $\begingroup$ If you use Quantity to represent values with units the comparison will work. $\endgroup$ May 17, 2020 at 19:30

2 Answers 2

3
$\begingroup$
$Version

(* "12.1.0 for Mac OS X x86 (64-bit) (March 14, 2020)" *)

As mentioned in the comment by Simon Woods, use Quantity

BNikkei = {{10.0032, 27 Days}, {10.0527, 22 Days}, {10.1283, 18 Days}} /. 
  x_*Days :> Quantity[x, "Days"];

Select[BNikkei, #[[2]] < Quantity[22, "Days"] &]

(* {{10.1283, Quantity[18, "Days"]}} *)

Alternatively, if you are certain that all units are "Days"

Select[BNikkei, QuantityMagnitude[#[[2]]] < 22 &]

(* {{10.1283, Quantity[18, "Days"]}} *)

However, for mixed units

BNikkei2 = Append[BNikkei, {10.1, Quantity[3, "Weeks"]}];

The first method works with mixed units

Select[BNikkei2, #[[2]] < Quantity[22, "Days"] &]

(* {{10.1283, Quantity[18, "Days"]}, {10.1, Quantity[3, "Weeks"]}} *)

The second method requires conversion

Select[BNikkei2 /. q_Quantity :> UnitConvert[q, "Days"], 
 QuantityMagnitude[#[[2]]] < 22 &]

(* {{10.1283, Quantity[18, "Days"]}, {10.1, Quantity[21, "Days"]}} *)

EDIT: The built-in conversions are

UnitConvert[Quantity[1, #], "Days"] & /@ {"Weeks", "Months", "Years"}

(* {Quantity[7, "Days"], Quantity[365/12, "Days"], Quantity[365, "Days"]} *)

UnitConvert[Quantity[90, "Days"], #] & /@ {"Weeks", "Months", "Years"}

(* {Quantity[90/7, "Weeks"], Quantity[216/73, "Months"], 
 Quantity[18/73, "Years"]} *)
$\endgroup$
2
  • $\begingroup$ Any pointers for this scenario. (a) define Days=1, (b) define Weeks=7 days, (c) define Month=30 days. So, I can 3 Months > 90 Days. ** Should I study UnitConvert[ more? ** Manuals seem sparse in this topic? $\endgroup$
    – prog9910
    May 17, 2020 at 20:44
  • $\begingroup$ ie. Do you have a recommendation for a book or website dealing with UnitConvert[ function?? (Units Overview on WLF MMA, I didn't know about. I'll try it first.] $\endgroup$
    – prog9910
    May 17, 2020 at 20:48
2
$\begingroup$

Days is not defined. At least on version 12.1 that I am using, Mathematica does not know what Days is.

Hence < will fail on it for the same reason that13 < 4 returns True, but 13 < 4 * x returns 13 < 4 * x when x is undefined.

Now, if you look at

expr = 22 Days;
First @ expr
(* 22 *)
Last @ expr
(* Days *)

Hence you could also do this

  BNikkei = {{10.0032, 27 Days}, {10.0527, 22 Days}, {10.1283, 18 Days}}
  Select[BNikkei, First @ (#[[2]]) < 22 &]

  (* {{10.1283, 18 Days}} *)

Another way

   Cases[BNikkei, {x_, y_} /; First[y] < 22 :> {x, y}]
  (* {{10.1283, 18 Days}} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.