我有一个包含三列的表格(typeofcrime,disctrict,year)
示例:
typeofcrime district year
HOMICIDE 092 2016
THEFT 053 2017
HOMICIDE 075 2016
ASSAULT 025 2014
我想计算每个地区和年份 HOMICIDES 的百分比。
这样的事情:
DISTRICT YEAR PERCENTAGE OF HOMICDE
075 2016 33%
092 2016 0%
025 2014 2%
我怎么能在PigLatin中做到这一点?
答案 0 :(得分:0)
所以你有一张桌子(A
),其中每一行代表一个犯罪事件,你想计算该地区和年份所有犯罪中每个地区和每年的杀人百分比,对吧?
您可以使用嵌套的foreach语句执行此操作:
B = GROUP A BY (district, year);
C = FOREACH B {
homicides = FILTER A BY typeofcrime == 'HOMICIDE';
GENERATE
FLATTEN(group) AS (district, year),
(float)COUNT(homicides)/(float)COUNT(A) AS homicidepercent;
};