Published
Edited
Mar 4, 2019
Insert cell
//switched to another document mid day class https://observablehq.com/@borneman/week-3-data-management-with-zebras
Insert cell
Insert cell
Insert cell
Insert cell
//similar to dataframes in R
Insert cell
Insert cell
z = require('https://bundle.run/zebras@0.0.11')
//import our zebras library. importing
Insert cell
Z = require('zebras')
//this works too but the example above also specifies the library
Insert cell
Insert cell
md`
> \`\`\`javascript
df = [
{columnOne: 1, columnTwo: 'A'},
{columnOne: 2, columnTwo: 'B'}
]
\`\`\`
`
Insert cell
//this is the way d3.csv is going to return the data
//each row is an array of obejcts.
//row 1 is {columnOne: 1 , columnTwo :A }//
Insert cell
Insert cell
md`
> \`\`\`javascript
columnOne = [1, 2]
columnTwo = ['A', 'B']
\`\`\`
`
Insert cell
//the actual column. if you call a column you will get ana rray of columns(not an array of rows)
Insert cell
Insert cell
rainAmounts = [
18.3,
19,
14.1
]
Insert cell
rainDates = Array.from({length: 3}, (_, i) => Date.now() - 1000 * 60 * 60 * 24 * i)
Insert cell
Insert cell
//can loop through my arrays in oder to take this list and turn it into an array of objects
Insert cell
Insert cell
newRainfall = {
let rainarray = []
for( var i =0 ; i<rainAmounts.length; i++) {
let currRow = {rainAmount: rainAmounts[i],
date: rainDates[i]}
rainArray.push(currRow)
}
return rainArray
}
// JESUS

Insert cell
//for( var i =0 ; i<rainAmounts.length; i++) {//match the index? make sure that things are the right lenght. if i want to make it more flexible

//newRainfall = {
let rainarray = []
for( var i =0 ; i<rainAmounts.length; i++) {
let currRow = {rainAmount: rainAmounts[i],//going to be an object, this is the current row
date: rainDates[i]} ) //rememeber we can access a given element by putting the index into the []// /
//now we have an object . an empty container for storing stuff.
rainDF.push({precipitation: rainAmounts [i], date: rainDates [i] })
} return rainDF
}

//MESSY NOTES
Insert cell
//both elements have 3 elements, 2 arrays
// return rainDF //this is going to be storing the object into the array and adding it into the array
//make sure my code is same as Carlos's
//* rainfall = {
// let rainDF = []
// for (var i=0; i<rainAmounts.length; i++) {
// rainDF.push({precipitation: rainAmounts[i], date: rainDates[i]})
// }
// return rainDF
//}
//above is Carlos code. compare

Insert cell
rainfall = {
let rainDF = []
for (var i=0; i<rainAmounts.length; i++) {
rainDF.push({precipitation: rainAmounts[i], date: rainDates[i]})
}
return rainDF
}
Insert cell
Insert cell
//this means we cant mutate a data frame, we are going to be making a brand new dataset./here he means we are combing
Insert cell
daySeries = ["Monday", "Tuesday", "Wednesday"]
Insert cell
z.addCol("day", daySeries, rainfall)
//we are adding a new column to the rainfall dataframe that is called "day" and takes the objects from the daySeries array
//the add.Col method is part of zebras library so this is the notation we would use to use the method
//see the page where we find the base example code.

//LOOK DEEPLY INTO THIS DATA FRAME. We have mixed data types
Insert cell
Insert cell
columns = Object.keys(rainfall[0])
Insert cell
Insert cell
shape = ({rows: rainfall.length, columns:Object.keys(rainfall[0]).length})
//we can assess the shape of things on our own .
//number elements is number rows
//number of properties in object is number of columns
Insert cell
rainfall.length
//number of rows
Insert cell
Insert cell
Insert cell
Insert cell
migrationUrl = "https://gist.githubusercontent.com/cesandoval/b834ac93c07e03ec5205843b97f68017/raw/90951b60444376eebfcbbee9beca00c083f489f6/API_SM.POP.NETM_DS2_en_csv_v2_10473747.csv"
Insert cell
migration = d3.csv(migrationUrl)
//this function allows us to read a csv using our D3 library. import the csv file from github. should we familiar.
Insert cell
Insert cell
Insert cell
Insert cell
z.head(5, migration)
//5 is n number of columns
Insert cell
Insert cell
Insert cell
Insert cell
migration.length
//get the length in rows of our table.
Insert cell
Insert cell
({rows: migration.length, columns:Object.keys(migration[0]).length})
Insert cell
Insert cell
//HERE I SWITCHED TO THE OTHER WEEK 3 DATA MANAGEMENT DOCUMENT.
//https://observablehq.com/d/ab30083953ae2220
Insert cell
Insert cell
firstRow = Object.values(migration[0])
Insert cell
lastRow = Object.values(migration[migration.length-1])
Insert cell
randomRow = Object.values(migration[Math.floor(Math.random() * migration.length)])
Insert cell
Insert cell
migrationColumns = Object.keys(migration[0])
Insert cell
Insert cell
numericalColumns = migrationColumns.slice(0, migrationColumns.length-4)
Insert cell
migrationParsed = z.parseNums(numericalColumns, migration)
Insert cell
Insert cell
z.head(5, migrationParsed)
Insert cell
Insert cell
countryName = z.getCol('Country Name', migrationParsed)
Insert cell
Insert cell
z.describe(z.getCol('1962', migrationParsed))
Insert cell
Insert cell
z.valueCounts(z.getCol('Indicator Code', migrationParsed))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
z.print([
{
minReturn: z.min(dailyReturns).toFixed(5),
maxReturn: z.max(dailyReturns).toFixed(5),
meanDailyReturn: z.mean(dailyReturns).toFixed(5),
medianDailyReturn: z.median(dailyReturns).toFixed(5),
dailyVolatility: z.std(dailyReturns).toFixed(5)
}
])
Insert cell
Insert cell
vegalite({
data: {values: dataRolling},
width: 700,
height: 350,
mark: {type: "line", strokeWidth: .9},
encoding: {
x: {field: "Date", type: "temporal"},
y: {field: "rollingVolatility", type: "quantitative", scale: {type:'linear'}}
}
})
Insert cell
Insert cell
Insert cell
Insert cell
dates = z.deriveCol(r=>Date.parse(r.Date), dataWithReturns)
Insert cell
dataDates = z.addCol('DateStamp', dates, dataWithReturns)
Insert cell
days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday']
Insert cell
daysOfWeek = z.deriveCol(x=>days[(new Date(x.DateStamp)).getDay()], dataDates)
Insert cell
dataDays = z.addCol('Day', daysOfWeek, dataDates)
Insert cell
dataGrouped = z.groupBy(x=>x.Day, dataDays)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dataPiped = z.pipe(
[
z.parseNums(['Open','High','Low','Close', 'Adj Close', 'Volume']),
z.getCol('Close'),
z.pctChange(),
z.mean()
]
)(data)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csv("https://gist.githubusercontent.com/nickslevine/b518a5fc788099d8955e77b4a4dc0628/raw/60edf99d70141a44c05fc73dff758c219f1684b4/sandp.csv")
Insert cell
dataParsed = z.parseNums(['Open','High','Low','Close', 'Adj Close', 'Volume'], data)
Insert cell
vegalite = require("@observablehq/vega-lite")
Insert cell
Insert cell
prices = z.getCol('Close', dataParsed)
Insert cell
dailyReturns = z.pctChange(prices)
Insert cell
dataWithReturns = z.addCol('DailyReturns', dailyReturns, dataParsed)

Insert cell
dataSorted = z.sortByCol('DailyReturns', 'asc', dataWithReturns)
Insert cell
rollingVolatility = z.rolling(z.std, 252, dailyReturns)
Insert cell
rollingMean = z.rolling(z.mean, 252, dailyReturns)
Insert cell
dataRolling = z.pipe([
z.addCol('rollingVolatility', rollingVolatility),
z.addCol('rollingMean', rollingMean),
])(dataWithReturns)
Insert cell
annualisedVolatility = z.deriveCol((x=>x.rollingVolatility * Math.sqrt(252)), dataRolling)
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