從韋恩圖看統(tǒng)計編程語言上的 merge/join

無論是SAS,R,Python,Stata,還是SPSS,關(guān)于merge(或者是各種形式的join,如inner join、Cartesian Product)數(shù)據(jù)集的操作原理都是上面的韋恩圖范疇之內(nèi),不同的只是代碼形式。萬變不離其宗。
學習之余來玩一個游戲:
現(xiàn)在我有7個點,或者直接是7個城市,希望你給我做一個每兩個城市之間連線的數(shù)據(jù)集,希望越簡單越好。
這里我們拿 SAS 來做,對于上面其他 4 種統(tǒng)計編程語言,有感興趣的小伙伴可以嘗試做一下。
首先我們隨意給定一個數(shù)據(jù)集:
data cities;
input pointid city $12.;
cards;
1 ? LosAngeles
2 ? Orlando
3 ? London
4 ? NewYork
5 ? Boston
6 ? Paris
7 ? Washington
8 ? LosAngeles
9 ? Orlando
10 ?London
;
run;
proc sql;
?create table discity ?as
?select distinct city
?from cities;
quit;
proc print data=discity;
?title "Distinct Cities";
?format city $12.;
run;

原理上很簡單,7 個城市之間任意兩點連線,是個排列組合問題,對于不重復的連線,應該是7*6/2 =21條。
我們是這么操作的:
proc sql;
? create table pair_cities as
? select f1.city as orig ,
? ? ? ? ?f2.city as dest
? ?from ?discity ?as f1 , discity as f2
? where f1.city ne ' ' & f1.city < f2.city
? order by f1.city, f2.city;
quit;
title 'All Possible Paired Places';
proc print data=pair_cities;
?format orig dest $12.;
run;
