Merge and aggregate datasets: Difference between revisions
→{{header|Prolog}}: Update both versions
(→{{header|Prolog}}: Use date atoms in XSB implementation) |
(→{{header|Prolog}}: Update both versions) |
||
Line 846:
Mean is Sum/Count.
test :-
bagof(summaryDates(Id, Lastname, LastDate),▼
fail.
test :-
bagof(summaryScores(Id, Lastname, ScoreSum, ScoreMean),▼
{{output}}
<pre>
summaryDates(2002,Gosling,2020-10-08)
summaryScores(1001,Hopper,17.4,5.8)
summaryScores(2002,Gosling,6.8,6.8)
summaryScores(4004,Wirth,15.4,7.7)
false.</pre>
Implemented using XSB Prolog:
<lang prolog>:- import bagMax/2, bagCount/2, bagSum/2, bagReduce/4 from aggregs.
:- import
:- import load_csv/2, add_cvt_type_hook/2 from proc_files.
?- add_cvt_type_hook(date,date_converter(_,_)).
patient(1001,'Hopper').▼
▲visit(3003,'2020-11-12',nan).
▲visit(4004,'2020-11-05',7.0).
date_converter(Atom,Date) :- date_string('YYYY-MM-DD',Date,Atom).
:- load_csv('visit.csv',visit(integer,date,float)).
:- load_csv('patient.csv',patient(integer,atom)).
summaryDates(Id, Lastname, LastDate) :-
bagMax(date_number(Id), LastDateNumber),
patient(Id,Lastname),
date_string('YYYY-MM-DD', date(Y,M,D), LastDate).
summaryScores(Id, Lastname, Sum, Mean) :-
bagSum(scores(Id), Sum),
bagCount(scores(Id), Count),
▲ Mean is Sum/Count.
test :-
test :-
/* Put hilog declarations together */
date_number(Id)(Number) :-
visit(Id,
julian_date(Number, Y, M, D, _, _, _).
scores(Id)(Score) :-
visit(Id, _, Score), Score>0.0. %% nan.0 is very small
:- hilog maximum.
Line 905 ⟶ 910:
sum(X,Y,Z) :- Z is X+Y.
:- hilog successor.
successor(X,_Y,Z) :- Z is X+1.</lang>
▲bagof(summaryDates(Id,Lastname,LastDate),
▲ summaryDates(Id,Lastname,LastDate),
▲bagof(summaryScores(Id, Lastname, ScoreSum, ScoreMean),
▲ summaryScores(Id, Lastname, ScoreSum, ScoreMean),
=={{header|PureBasic}}==
|