Platform
Resources
Pricing
Sign in
Get started
Christopher Pietsch
Visualization, Prototypes, ML, Data wrangling and Art.
Workspace
Fork
Public
By
Christopher Pietsch
Edited
Sep 22, 2023
Fork of
Adding Columns to Data
Insert cell
Insert cell
Insert cell
import
{
fixedStates
}
from
"@observablehq/excel-data-loading"
Insert cell
Insert cell
laserStrikes
=
addColumn
(
fixedStates
,
'year'
,
row
=>
row
[
'Incident Date'
]
.
getFullYear
(
)
)
Insert cell
Insert cell
d3
.
groups
(
laserStrikes
,
d
=>
d
.
year
)
Insert cell
Insert cell
strikesByYear
=
d3
.
groups
(
laserStrikes
,
d
=>
d
.
year
)
.
map
(
d
=>
{
return
{
year
:
d
[
0
]
,
count
:
d
[
1
]
.
length
}
}
)
Insert cell
Insert cell
viewof
table
=
Inputs
.
table
(
strikesByYear
,
{
sort
:
"year"
}
)
Insert cell
Insert cell
Plot
.
plot
(
{
marks
:
[
Plot
.
barY
(
strikesByYear
,
{
x
:
"year"
,
y
:
"count"
,
fill
:
"steelblue"
}
)
,
Plot
.
ruleY
(
[
0
]
)
]
}
)
Insert cell
Insert cell
laserStrikesCleaned
=
laserStrikes
.
filter
(
d
=>
d
.
year
>
2016
)
Insert cell
Insert cell
Plot
.
plot
(
{
marks
:
[
Plot
.
barY
(
laserStrikesCleaned
,
Plot
.
groupX
(
{
y
:
"count"
}
,
{
x
:
"year"
,
fill
:
"steelblue"
}
)
)
,
Plot
.
ruleY
(
[
0
]
)
]
}
)
Insert cell
Insert cell
Insert cell
Insert cell
function
addColumn
(
tableData
,
newColumnName
,
newColumnFunction
)
{
return
tableData
.
map
(
row
=>
{
return
{
...
row
,
[
newColumnName
]
:
newColumnFunction
(
row
)
}
}
)
}
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.
Try it for free
Learn more
Compare fork
Fork
View
Export
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
laserStrikes
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
strikesByYear
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
table1
Edit
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
table
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
laserStrikesCleaned
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
Edit
Add comment
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML
addColumn
Add comment
Copy import
Select
Duplicate
Copy link
Embed
Delete
JavaScript
Markdown
HTML