4
$\begingroup$

Here is a simple dataset:

data = Dataset[<|"Joe" -> <|"Day" -> "Mon", "Rate" -> 11|>, 
   "Sue" -> <|"Day" -> "Tue", "Rate" -> 13|>, 
   "Paul" -> <|"Day" -> "Wed", "Rate" -> 10|>, 
   "Leslie" -> <|"Day" -> "Thurs", "Rate" -> 15|>, 
   "Sam" -> <|"Day" -> "Fri", "Rate" -> 9|>|>]

I'd like to select the rows where, for example, 10<="Rate"<=14 and then total those amounts. I've tried various methods using, for example, Select, but I can't find anything that Mathematica will accept.

$\endgroup$

2 Answers 2

7
$\begingroup$

Assuming that you intend to use a Dataset with named rows (and being aware that this means that each row must have a unique name), one way is:

(*define the dataset*)
data = Dataset[<|"Sue" -> <|"Day" -> "Tue", "Rate" -> 13|>, 
   "Paul" -> <|"Day" -> "Wed", "Rate" -> 10|>, 
   "Leslie" -> <|"Day" -> "Thurs", "Rate" -> 15|>, 
   "Sam" -> <|"Day" -> "Fri", "Rate" -> 9|>|>]

(*illustrative example:  just applying the operation and return only the Rate column*)
data[Select[10 <= #["Rate"] <= 14 &], "Rate"] 


(*Answer to the question: apply the operation and return only the total*)
data[Select[10 <= #["Rate"] <= 14 &], "Rate"][Total] 

Alternatively, you can also define your search as a Query, and try something like:

selectQ = Query[Select[10 <= #Rate <= 14 &], "Rate"];
Total@selectQ[data]

(note that either #["Rate"] or #Rate can be used to denote the named column.)

--Edit--

As J. M.'s ennui pointed out in the comments, there is also the ability to apply Total using a right-composition (/*). This allows you to incorporate the Total operation into the Query, like follows:

totalQ = Query[Select[10 <= #Rate <= 14 &] /* Total, "Rate"];
totalQ[data]

(You are also free to use the Between function in any of these, as demonstrated in J.M.'s comment, if that seems more natural.)

$\endgroup$
2
  • 2
    $\begingroup$ I tend to prefer using Between[] for situations like this: data[Select[Between[#Rate, {10, 14}] &] /* Total, "Rate"] $\endgroup$ Feb 26, 2021 at 7:07
  • $\begingroup$ Between is rather slick, and I learned something about the use of the right-composition \* $\endgroup$ Feb 26, 2021 at 14:31
0
$\begingroup$

A simple and feasible method:

data = Dataset[{"Sue" -> <|"Day" -> "Tue", "Rate" -> 13|>, 
   "Paul" -> <|"Day" -> "Wed", "Rate" -> 10|>, 
   "Leslie" -> <|"Day" -> "Thurs", "Rate" -> 15|>, 
   "Sam" -> <|"Day" -> "Fri", "Rate" -> 9|>}]
Cases[data, 
  HoldPattern[_ -> Association[__, "Rate" -> x_]] /; 10 <= x <= 14 :> 
   List[x]] // Total
$\endgroup$
2
  • 3
    $\begingroup$ Your dataset is a bit broken; I believe OP wanted the "named row" form, so it should be an association with keys Sue, Paul, etc. rather than a list of rules. $\endgroup$ Feb 26, 2021 at 0:57
  • $\begingroup$ @JoshuaSchrier +1 Thank you. I think your understanding is correct. $\endgroup$ Feb 26, 2021 at 1:26

Your Answer

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

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