Published
Edited
Nov 2, 2021
Importers
Insert cell
Insert cell
### TODO
- [OK] live data
- [OK] cleaned weekly new death data
- [OK]padding 2020 first 2 weeks
- New structure: add top countries by region contributing the most deaths
- Data of five selected countries: India, Indonesia, China, Malaysia, Singapore
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
statusCode = fetch('https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/jhu/new_deaths.csv').then(function(response) {
console.log(response.status); // returns 200
return response.status;
});
Insert cell
Insert cell
viewof table = aq // viewof shows the table view, but assigns the table value
.from(rawData)
.view({ height: 240 }) // set maximum height of table viewer in pixels
Insert cell
table.get('date', table.totalRows()-1)
Insert cell
// viewof table = aq // viewof shows the table view, but assigns the table value
// .fromCSV(await FileAttachment('new_cases.csv').text())
// .view({ height: 240 }) // set maximum height of table viewer in pixels
Insert cell
viewof table2 = aq // viewof shows the table view, but assigns the table value
.fromCSV(await FileAttachment('new_deaths.csv').text())
.view({ height: 240 }) // set maximum height of table viewer in pixels
Insert cell
Insert cell
viewof locations = aq // viewof shows the table view, but assigns the table value
.fromCSV(await FileAttachment('locations.csv').text()).select("location", "continent")
.view({ height: 240 }) // set maximum height of table viewer in pixels
Insert cell
// get unique continent list
regionList = [...new Set(locations.select('continent')._data.continent.data)].slice(1).concat("date")
Insert cell
viewof dt = table.select(regionList).view()
Insert cell
Insert cell
viewof data_ = dt.derive({dateStr: d=> d.date})
.derive({date: d=> op.parse_date(d.date)})
.derive({year: d=> op.year(d.date)})
.derive({week: d=> op.week(d.date)}) // Sunday-based week number https://uwdata.github.io/arquero/api/op.html
.derive({yearWeek: d=> d.year + "-"+ d.week}).view()
Insert cell
Insert cell
viewof data = data_.impute({ Europe: () => 0 })
.impute({ Africa: () => 0 })
.impute({ "North America": () => 0 })
.impute({ "South America": () => 0 })
.impute({ "Oceania": () => 0 })
.derive({ NorthAmerica: d => d["North America"]})
.derive({ SouthAmerica: d => d["South America"]})
.view()
Insert cell
// data_.impute({ Europe: () => 0 })
// .impute({ Africa: () => 0 })
// .impute({ "North America": () => 0 })
// .impute({ "South America": () => 0 })
// .impute({ "Oceania": () => 0 })
// .view()
Insert cell
// weeklyNum2 = data.fold(data.columnNames().splice(0,7))
// .rename({ key: 'region' })
// .rename({ value: 'dailyNum' })
// .groupby('yearWeek')
// .pivot('region', { weeklyNum: d => op.sum(d.dailyNum) }).view()
Insert cell
viewof weeklyNum = data.fold(data.columnNames().slice(0,6))
.rename({ key: 'region' })
.rename({ value: 'dailyNum' })
.groupby('yearWeek')
.pivot('region', { weeklyNum: d => op.sum(d.dailyNum) })
.derive({ NorthAmerica: d => d["North America"]}) // .rename doesn't work on col names with white spaces
.derive({ SouthAmerica: d => d["South America"]})
.select(weeklyNumCol)
.view()
Insert cell
Insert cell
viewof cumu_weeklyNum = weeklyNum
.derive({ cumuAsia: aq.rolling(d => op.sum(d.Asia)) })
.derive({ cumuEurope: aq.rolling(d => op.sum(d.Europe))})
.derive({ cumuNorthAmerica: aq.rolling(d => op.sum(d.NorthAmerica)) })
.derive({ cumuSouthAmerica: aq.rolling(d => op.sum(d.SouthAmerica)) })
.derive({ cumuAfrica: aq.rolling(d => op.sum(d.Africa)) })
.derive({ cumuOceania: aq.rolling(d => op.sum(d.Oceania))})
.view()
Insert cell
cumu_weeklyNum.columnNames().splice(1,12)
Insert cell
data.columnNames().splice(0,6)
Insert cell
Insert cell
// add week 0 since op.week defines all days in a new year preceding the first Sunday are considered to be in week 0.
viewof firstTwoRows = aq.table({ yearWeek: ["2020-0", "2020-1", "2020-2"], Africa: [0,0,0], Asia: [0,0,0], Europe: [0,0,0], NorthAmerica: [0,0,0], Oceania: [0,0,0], SouthAmerica: [0,0,0], cumuAfrica: [0,0,0], cumuAsia: [0,0,0], cumuEurope: [0,0,0], cumuNorthAmerica: [0,0,0], cumuOceania: [0,0,0], cumuSouthAmerica: [0,0,0], }).view()
Insert cell
cumu_weeklyNum.view()
Insert cell
viewof weeklyNumWithPadding = firstTwoRows.concat(cumu_weeklyNum).view()
Insert cell
cumuCol = (["yearWeek"]).concat(weeklyNumWithPadding.select(aq.startswith("cumu")).columnNames())
Insert cell
viewof cumuWeeklyNumWithPadding = weeklyNumWithPadding
.select(cumuCol)
.rename(aq.names(weeklyNumWithPadding.columnNames().slice(0,7)))
.view()
Insert cell
viewof cumuTable = cumuWeeklyNumWithPadding.fold(cumuWeeklyNumWithPadding.columnNames().splice(1,7))
.rename({ key: 'region' })
.rename({ value: 'cumuDeaths' })
.derive({id: d=> d.yearWeek + d.region})
.view()
Insert cell
regionList2 = [...new Set(locations.select('continent')._data.continent.data)].slice(1).concat("yearWeek")
Insert cell
viewof newTable = weeklyNumWithPadding
.select(weeklyNumWithPadding.columnNames().slice(0,7))
.fold(weeklyNumWithPadding.columnNames().splice(1,6)) // expect yearWeek
.rename({ key: 'region' })
.rename({ value: 'deaths' })
.derive({id: d=> d.yearWeek + d.region})
.view()
Insert cell
// long table
viewof joinedTable = newTable.join_full(cumuTable, "id")
.select(["yearWeek_1", "region_1", "deaths", "cumuDeaths"])
.select(aq.names(["yearWeek", "region", "deaths", "cumuDeaths"]))
.view()
Insert cell

maxDeaths = joinedTable.orderby(aq.desc('deaths'))
.select("deaths").object().deaths

Insert cell
weekMap = joinedTable.groupby('yearWeek')
.objects({ grouped: true })
Insert cell
// weekMap = weeklyNumWithPadding.fold(weeklyNumWithPadding.columnNames().splice(1,12))
// .rename({ key: 'region' })
// .rename({ value: 'deaths' })
// .groupby('yearWeek')
// .objects({ grouped: true })
Insert cell
// weekMapWithPadding = weeklyNumWithPadding.fold(weeklyNumWithPadding.columnNames().splice(1,12))
// .rename({ key: 'region' })
// .rename({ value: 'deaths' })
// .groupby('yearWeek')
// .objects({ grouped: true })
Insert cell
desiredData = [...weekMap].map(d=>d[1])
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