//what values should we convert? fill in the blanks
//think about what data fields we want to return --- not all are cloned automatically!
};
}
)
//the => Arrow function is a shorthand way of writing a function
//It is equivalent to d = function(){
// ...
// return a;
//}
//a function is called on every element in array titanic2, and creates a new array called titanicMap
//for every element in titanicMap, the age key is assigned a value corresponding to the numerical value of titanic2
titanicMap=titanic.map((d)=>{
vara=d;
a.Age=+d.Age;
a.SibSp=+d.SibSp;
a.Parch=+d.Parch;
a.Fare=+d.Fare;
//remember what other fields we want to add in
returna;
})
//Let's get the max age of who was on the titanic
//try creating a variable called ageMax and find the age of the oldest person on the titanic
ageMax={
//write a loop here
letmaxAge=0;
letname="";
for(leti=0;i<titanicMap.length;i++){
if(titanicMap[i].Age>maxAge){
maxAge=titanicMap[i].Age;
name=titanicMap[i].Name;
}
}
returnmaxAge+" "+name;
}
{
//Let's do this with d3.mean() now. We'll first have to use map to create an array with just the ages.
letages=titanicMap.map(function(d){
return+d.Age;
});
returnd3.mean(ages);
}
//return your answers here
{
//Let's do this with d3.mean() now. We'll first have to use map to create an array with just the ages.
letages=titanicMap.map(function(d){
return+d.Age;
});
return"Median: "+d3.median(ages);
}
//return your answers here
{
//Let's do this with d3.mean() now. We'll first have to use map to create an array with just the ages.
letages=titanicMap.map(function(d){
return+d.Age;
});
return"Mode: "+d3.mode(ages);
}
firstClass=titanicMap.filter(function(d){
returnd.CClass==1});
"The number of passengers in first class is: "+firstClass.length
//Try it here
{
letfirstClassAges=firstClass.map(function(d){
return+d.Age;
});
return(
"The average age in first class is: "+Math.round(d3.mean(firstClassAges))
);
}
//try to find the answer here for second class
//hint: you can use d3.mean to find the average age of passengers in second class
{
letsecondClass=titanicMap.filter(function(d){
returnd.CClass==2;
});
letsecondAges=secondClass.map(function(d){
return+d.Age;
});
returnd3.mean(secondAges);
}
//try to find the answer here for third class
//hint: you can use d3.mean to find the passengers in third class
//try to find the answer here for second class
//hint: you can use d3.mean to find the average age of passengers in second class
{
letthirdClass=titanicMap.filter(function(d){
returnd.CClass==3;
});
letthirdAges=thirdClass.map(function(d){
return+d.Age;
});
returnd3.mean(thirdAges);
}
functioncalculateTicket(){
//calculate the ticket inside this function
letticket=0;
letname="";
for(leti=0;i<titanicMap.length;i++){
if(titanicMap[i].Fare>ticket){
ticket=titanicMap[i].Fare;
name=titanicMap[i].Name;
}
}
returnname+" paid: $"+ticket;
}
calculateTicket();
vl.markTick()
.data(titanicMap)
.encode(
vl.x().fieldQ('Fare')
)
.render()
//follow along here
vl
.markBar()
.data(titanicMap)
.encode(vl.x().fieldQ("Age").bin({step:20}),
vl.y().count())
.render()
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.