Published
Edited
Apr 3, 2019
Insert cell
Insert cell
Insert cell
Insert cell
// Load a file locally
viewof text = html`<input type=file * ">`
Insert cell
Insert cell
// your code here
tweetsStrings = Files.text(text)
Insert cell
tweetsDF = Object.values(JSON.parse(tweetsStrings))
Insert cell
count = {
let countArray = []
let groupedLocations = z.groupBy(x => x.cleanLocations, newTweets)
for (const key of Object.keys(groupedLocations)) {
countArray.push({location: key, count: groupedLocations[key].length})
}
return countArray
}
Insert cell
countSorted = z.sortByCol('count', 'des', count)
Insert cell
newTweets = {
let newLocations = []
let locations = z.getCol("location", tweetsDF)
for (let id in locations) {
if (locations[id].toLowerCase().includes('boston')){
newLocations.push('Boston, MA')
} else if (locations[id].toLowerCase().includes('waltham')){
newLocations.push('Waltham, MA')
} else if (locations[id].toLowerCase().includes('cambridge')){
newLocations.push('Cambridge, MA')
} else if (locations[id]==""){
newLocations.push('NULL')
} else {
newLocations.push(locations[id])
}
}
return z.addCol("cleanLocations", newLocations, tweetsDF)
}
Insert cell
newTweets
Insert cell
vegalite({
data: {values: countSorted.slice(0,15)},
mark: "bar",
encoding: {
x: {field: "location", type: "nominal", sort:'*'},
y: {field: "count", type: "quantitative"},
color: {"field": "location", "type": "nominal"}
}
})
Insert cell
Insert cell
// your code here
notNull = z.filter(r => r.lat != null, tweetsDF)
Insert cell
vegalite({
data: {values: notNull},
mark: "circle",
encoding: {
x: {field: "lon", type: "quantitative",scale: {domain:[-71.3,-70.8]}},
y: {field: "lat", type: "quantitative",scale: {domain:[42.3,42.5]}}
}
})
Insert cell
vegalite({
data: {values: notNull},
projection: {type:"albersUsa"},
mark: "circle",
encoding: {
longitude: {field: "lon", type: "quantitative"},
latitude: {field: "lat", type: "quantitative"}
},
size:{value:10},
config:{view:{stroke:"transparent"}}
})
Insert cell
Insert cell
// Load a file locally
viewof hashtag_text = html`<input type=file * ">`
Insert cell
Insert cell
// your ciode here
tweetsStrings2 = Files.text(hashtag_text)
Insert cell
tweetsDF_temp = Object.values(JSON.parse(tweetsStrings2))
Insert cell
tweetsDF2 = {
let obj = []
for (let i in tweetsDF_temp){
obj.push(tweetsDF_temp[i][1])}
return obj
}
Insert cell
count_unclean = {
let countArray = []
let groupedLocations = z.groupBy(x => x.location, tweetsDF2)
for (const key of Object.keys(groupedLocations)) {
countArray.push({location: key, count: groupedLocations[key].length})
}
return countArray
}
Insert cell
countSorted_unclean = z.sortByCol('count', 'des', count_unclean)
Insert cell
newTweets2 = {
let newLocations = []
let locations = z.getCol("location", tweetsDF2)
for (let id in locations) {
if (locations[id].toLowerCase().includes('caracas')){
newLocations.push('Caracas, Venezuela')
} else if (locations[id].toLowerCase()=='venezuela'){
newLocations.push('Venezuela')
}else if (locations[id].toLowerCase().includes('miami')){
newLocations.push('Miami, FL')
} else if (locations[id].toLowerCase().includes('bogotá')){
newLocations.push('Bogotá, Colombia')
} else if (locations[id].toLowerCase().includes('bogota')){
newLocations.push('Bogotá, Colombia')
} else if (locations[id].toLowerCase()=='florida'){
newLocations.push('Florida, USA')
} else if (locations[id].toLowerCase()=='usa'){
newLocations.push('United States')
} else if (locations[id]==""){
newLocations.push('NULL')
} else {
newLocations.push(locations[id])
}
}
return z.addCol("cleanLocations", newLocations, tweetsDF2)
}
Insert cell
count2 = {
let countArray = []
let groupedLocations = z.groupBy(x => x.cleanLocations, newTweets2)
for (const key of Object.keys(groupedLocations)) {
countArray.push({location: key, count: groupedLocations[key].length})
}
return countArray
}
Insert cell
countSorted2 = z.sortByCol('count', 'des', count2)
Insert cell
Insert cell
// your code here
notNull2 = z.filter(r => r.lat != null, tweetsDF2)
Insert cell
{
for (let i in notNull2){
notNull2[i]["num_hash"]=notNull2[i].hashtags.length}
return notNull2
}
Insert cell
vegalite({
data: {values: notNull2},
mark: "circle",
encoding: {
x: {field: "lon", type: "quantitative"},
y: {field: "lat", type: "quantitative"},
color: {field: "num_hash", type: "nominal",title:"Number of Hashtags"},
}
})
Insert cell
Insert cell
Insert cell
Insert cell
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