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
rawData= d3.csv("https://raw.githubusercontent.com/owid/covid-19-data/master/public/data/jhu/new_deaths.csv")
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.select("Singapore", "date").view({ height: 240 })
Insert cell
lastDate = table.get("date", table.totalRows() - 1)
Insert cell
Insert cell
viewof lastDateTable = table.params({cv: lastDate}).filter((d, $) => d.date==$.cv).select(regionList).view()
Insert cell
// OCT 3 - OCT 9
viewof tLast = table.filter(d => d.date <= "2021-10-09" && d.date > "2021-10-02").select(regionList).view()
Insert cell
tLast.rollup({ total: d => op.sum(d.India) }).view()
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
regionList = ["India", "Indonesia", "China", "Malaysia", "Singapore"].concat("date")
Insert cell
viewof dt = table.select(regionList).view()
Insert cell
Insert cell
data.column('yearWeek').get
Insert cell
viewof dd = data.select('yearWeek', 'date')
.groupby("yearWeek")
.derive({date2: d => op.any(d.date)}) // get first date of the week
.dedupe('date2')
.select('yearWeek', 'date')
.view()
Insert cell
dd.objects().map(d => d.date)
Insert cell
dateObj = {
let yearWeek = dd.objects().map(d => d.yearWeek)
let date = dd.objects().map(d => d.date)
let dateObj = {}
for (let i = 0; i < yearWeek.length; i++) {
dateObj[yearWeek[i]] = date[i]
}

return dateObj
}
Insert cell
function yearWeekLookUp(yearWeekStr) {
return dateObj[yearWeekStr]
// return dd.filter(d => d.yearWeek === aq.escape(yearWeekStr)).array('date')
}
Insert cell
yearWeekLookUp("2020-8")
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
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) })
.select(weeklyNumCol)
.view()
Insert cell
Insert cell
// cumu_weeklyNum.columnNames().splice(1,12)
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"], India: [0,0,0], Indonesia: [0,0,0], China: [0,0,0], Malaysia: [0,0,0], Singapore: [0,0,0], cumuIndia: [0,0,0], cumuIndonesia: [0,0,0], cumuChina: [0,0,0], cumuMalaysia: [0,0,0], cumuSingapore: [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,6))
.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
weeklyNumWithPadding.columnNames().slice(0,6)
Insert cell
viewof newTable = weeklyNumWithPadding
.select(weeklyNumWithPadding.columnNames().slice(0,6))
.fold(weeklyNumWithPadding.columnNames().splice(1,5)) // 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