Published
Edited
Mar 7, 2019
Insert cell
Insert cell
Insert cell
Insert cell
// Naming URL

migrationurl=("https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/6dff0d45ed26352a7a3c7afb4ace0bad1ce8ba20/MIG_18022019163824069.csv")
Insert cell
// Loading URL
migration = d3.csv(migrationurl)
Insert cell
Insert cell
// Identifying the columns
columns= Object.keys(migration[0])
Insert cell
// Identifying unique country names:
z.unique(z.getCol('Country of birth/nationality', migration))

Insert cell
Insert cell
//This is just me getting a sense of the data
z.describe(z.getCol('C02', migration))
Insert cell
//Displaying the data frame
z.head(10,migration)
Insert cell
// Grouping By Types of Flows
flowsgrouped = z.groupBy (x => x.Variable, migration)
Insert cell
// Grouping by Country
countrygrouped = z.groupBy ( x => x.Country, migration)
Insert cell
// Grouping inflows by country of birth
inflowSum = z.gbSum ("Value", z.groupBy (x=>x['CO2'], flowsgrouped['Inflows of foreign population by nationality'], migration))
Insert cell
// Grouping outflows by country of birth
outflowSum= z.gbSum ("Value", z.groupBy (x=>x['CO2'], flowsgrouped['Outflows of foreign population by nationality'],migration))
Insert cell
// Grouping stock of foreign-born population by country of birth
stockSum = z.gbSum ("Value", z.groupBy (x=>x['CO2'], flowsgrouped['Stock of foreign-born population by country of birth'], migration))
Insert cell
// Joining Inflow with Outflow by Country
migJoin= z.merge(inflowSum, outflowSum, 'group', 'group', '_inflow', '_outflow')
Insert cell
// Joining previous join with stock population
migJoin2= z.merge(migJoin, stockSum, 'group', 'group')
Insert cell
Insert cell
//Creating loop to make data frame
dfFlows= {
let migrationtable = []
for (let i=0; i< 210; i++)
{
let newRow = {}
newRow['Country'] = countrycode [i];
newRow ['Summed Inflows'] = inflowSum[i].sum;
newRow ['Summed Outflows'] = outflowSum[i].sum;
newRow ['Summed Stock of Foreign-Born'] = stockSum[i].sum;
migrationtable.push(newRow)
}
return migrationtable;
}

Insert cell
//Printing the data frame

z.print (dfFlows)
Insert cell
Insert cell
Insert cell
//loading URL
compareMigration = d3.csv (newurl)
Insert cell
// Looking at column names
columnName = Object.keys(compareMigration[0])
Insert cell
// Only keeping Country Codes and the Continent
comparefiltered= z.pickCols(['Continent_Name', 'Three_Letter_Country_Code'], compareMigration)
Insert cell
// Join your datasets by Country Code
MigrationContinent= z.merge(dfFlows, comparefiltered, "Country", "Three_Letter_Country_Code", dfFlows, comparefiltered)
Insert cell
// Getting rid of undefined values
MigrationContinent1 = z.filter(r=>r.Continent_Name !=undefined, MigrationContinent)
Insert cell
// Grouping inflows by continent
continentInflow = z.gbSum("Summed Inflows", z.groupBy(x => x.Continent_Name, MigrationContinent1))
Insert cell
// Grouping outflows by continent
continentOutflow = z.gbSum( "Summed Outflows", z.groupBy(x => x.Continent_Name, MigrationContinent1))
Insert cell
// Grouping stock of population by continent
continentStock = z.gbSum( "Summed Stock of Foreign-Born", z.groupBy(x => x.Continent_Name, MigrationContinent1))
Insert cell
Insert cell
df_continent_summed = {
let continenttable = []
for (let i=0; i< 6; i++)
{
let Row = {}
Row['Continent'] = continent[i];
Row ['Inflows Summed'] = continentInflow[i].sum;
Row ['Outflows Summed'] = continentOutflow[i].sum;
Row ['Stock of Foreign-Born Summed'] = continentStock[i].sum;
continenttable.push(Row)
}
return continenttable;
}
Insert cell
// Printing inflows, outflows and stock of foreign born population by continent
z.print (df_continent_summed)
Insert cell
Insert cell
// Filtering for only 2016 data and stock of foreign0born population
stock2016 = z.filter(x=>x['Year']=='2016' && x['Variable']=='Stock of foreign-born population by country of birth', migration)
Insert cell
// Grouping by country of birth
birth2016 = z.gbSum("Value", z.groupBy(x => x["Country of birth/nationality"], stock2016))
Insert cell
// Grouping by high immigration
highImmigration = z.filter(x=>x["sum"] >100000, birth2016)
Insert cell
// Grouping by medium immigration
mediumImmigration = z.filter(x=>x['sum']<100000 && x['sum']>50000, birth2016)
Insert cell
Insert cell
// Creating country array for low immigration data frame
count = z.unique(z.getCol("group", lowImmigration))
Insert cell
df_lowImmigration = {
let lowtable = []
for (let i=0; i< count.length; i++)
{
let Row = {}
Row['Country'] = count[i];
Row ['Low Immigration'] = lowImmigration[i]['sum'];
lowtable.push(Row)
}
return lowtable;
}
Insert cell
z.print(df_lowImmigration)
Insert cell
// Creating country array for medium immigration data frame
count1 = z.unique(z.getCol("group", mediumImmigration))
Insert cell
df_mediumImmigration = {
let lowtable = []
for (let i=0; i< count1.length; i++)
{
let Row = {}
Row['Country'] = count1[i];
Row ['Medium Immigration'] = mediumImmigration[i]['sum'];
lowtable.push(Row)
}
return lowtable;
}
Insert cell
z.print(df_mediumImmigration)
Insert cell
//Creating country array for high immigration data frame
count2 = z.unique(z.getCol("group", highImmigration))
Insert cell
df_highImmigration = {
let hightable = []
for (let i=0; i< count2.length; i++)
{
let Row = {}
Row['Country'] = count2[i];
Row ['High Immigration'] = highImmigration[i]['sum'];
hightable.push(Row)
}
return hightable;
}
Insert cell
z.print(df_highImmigration)
Insert cell
Insert cell
// Creating array of countries
count3 = z.unique(z.getCol("group", migJoin))
Insert cell
// Creating data frame showing inflows minus outflows of people by country of birth/origin
df_subtractionFlows={
let subtraction = []//create new array (table) to push results to
for (var i = 0; i<210; i++){//create a loop for each country
let row = {};
row['Country'] = count3[i];
row['Inflow - Outflow']= migJoin[i]['sum_inflow']- migJoin[i]['sum_outflow'];//print the difference between the inflow and the outflow
subtraction.push(row);
}
return subtraction;
}

Insert cell
Insert cell
Insert cell
Insert cell
// Loading z
z = require('https://bundle.run/zebras@0.0.11')
Insert cell
// Defining d3
d3=require("d3")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more