Public
Edited
Jan 24, 2023
Insert cell
Insert cell
Insert cell
Insert cell
// tidy แล้ว
table1.view()
Insert cell
// table2 มี type ปนระหว่าง case, pop
table2.view()
Insert cell
// table3 เก็บ case, pop ใน rate ด้วยการต่อกัน
table3.view()
Insert cell
// table4 แยกเก็บเป็น 2 table
table4a.view()
Insert cell
table4b.view()
Insert cell
Insert cell
// คำนวณ rate
table1
.derive({ rate: d=>d.cases/d.population * 10000 })
.view()
Insert cell
// เทียบ cases แต่ละปี (ไม่สนประเทศ)
table1
.groupby('year')
.rollup({ cases: op.sum('cases') })
.view()
Insert cell
// viz การเพิ่มแต่ละปี ของแต่ละประเทศ
vl.markLine({ point: true })
.data(table1)
.encode(
vl.x().fieldQ('year').axis({ tickMinSteep: 1, format: 'd' }),
vl.y().fieldQ('cases'),
vl.color().fieldN('country')
)
.render()
Insert cell
// fold (pivot longer)
table4a.view()
Insert cell
viewof fold4a = table4a
.fold(['1999', '2000'], {as: ['year', 'cases']})
.view()
Insert cell
// same for population
viewof fold4b = table4b
.fold(['1999', '2000'], { as: ['year', 'population'] })
.view()
Insert cell
// pivot wider
table2.view(8)
Insert cell
table2
.groupby('country', 'year')
.pivot('type', 'count') // เก็บชื่อ col, ค่า value
.view()
Insert cell
table3.view()
Insert cell
// 2 step
// - op.split เพื่อแยก
// spread() เพื่อตั้งชื่อ
table3
.spread({ rate: d=> op.split(d.rate, '/') },
{ as: ['cases', 'population'] }
)
.view()
Insert cell
// แปลง string เป็น int
table3
.spread({ rate: d=> op.split(d.rate, '/') },
{ as: ['cases', 'population'] }
)
.derive({ cases: d=>+d.cases,
population: d=>+d.population
})
.view()
Insert cell
// regex แยก string
viewof table5 = table3
.spread({ year: d=> op.match(d.year, /.{2}/g) },
{ as: ['century', 'year'] }
).view()
Insert cell
// รวมกลับด้วย derive
table5
.derive({ new: d=> `${d.century}_${d.year}` },
{ before: 'year' })
.select(aq.not('year', 'century')) // ไม่ใช้แล้ว
.view()
Insert cell
Insert cell
// null, undefined ถือว่า missing แบบชัดแจ้ง
// 2016 Q1 ถือว่าหาย
viewof stocks = aq.table({
year: [2015, 2015, 2015, 2015, 2016, 2016, 2016],
qtr: [ 1, 2, 3, 4, 2, 3, 4],
return: [1.88, 0.59, 0.35, null, 0.92, 0.17, 2.66]
}).view()
Insert cell
// ทำที่หายให้ explicit ด้วย pivot
stocks
.groupby('qtr')
.pivot('year', 'return')
.view()
Insert cell
// impute ข้ามไปละกัน
Insert cell
// Tuberculosis Case Study ก็ข้าม
Insert cell
// Housiing Crime ก็ข้าม
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