How to GraphXR
5. Aggregate, Merge, and f(x)
In this Session...Before you begin...
* Using Transforms:

* f(x) and Link to reformat and reorganize data.

* Aggregate to enumerate and/or evaluate nodes or edges.

* Merge to merge nodes or edges.
To follow along, download the files:


and (opens in a new tab)
1How To GraphXR 5. Aggregate and Merge
2Before You Begin...

Ideally, you'll have worked through Module 4. Link and Filter. If you're starting here, and you want to follow along, you'll need to:

* Log in to GraphXR, create a Project, and open its graph space. , Download the file (opens in a new tab), which contains the Game of Thrones data we'll use in our tutorials., Drag and drop the file 05_START.graphxr onto the space.
3So far, we've extracted a House category from the Characters.csv data, created a BELONGS_TO relationship with Character nodes, and a SPOKE relationship between Characters and the Lines they spoke.
4Now we want to link lines of dialog with the broadcast Episodes they were spoken on.
We can do this using Merge and Aggregate transforms. Drag and drop Episodes.csv onto the graph space, creating the new Episodes category and properties.
5The Episodes.csv file contains the titles, descriptions, air dates, episode numbers, seasonnumbers, and viewership for each broadcast episode. We can now connect lines of dialog to corresponding episodes.
6We have a little problem, though. In Lines, season and episode number are combined into the single string seasonEpisode. In Episodes, they occupy separate properties.
7To fix this, we'll use the Transform panel and f(x) tab. It lets us run javascript formulas on properties of a category or relationship. Presets are included, but for this we'll need to entera custom formula. Select Episodes and episodeNumber.
8Enter seasonEpisode as the new property name, and enter the custom formula :

(propVal,props) => 'S'+props.seasonNumber+'E'+props.episodeNumber

An example result is automatically displayed under the New Property Name.
9Click Run. You can scroll to the bottom of the panel to view the results.
10Now that the property values match, we can go to the Link transform. In both Episodes and Lines, select the seasonEpisode property, and click Run to link nodes with matching properties via edges of a new SPOKEN_ON relationship.
11As we explore patterns in more detail, we can use Aggregate to add new properties to the graph and Merge to simplify and clarify the graph.
12We want to add a totalLines property-the total number of lines spoken-to each Episode node. Lines nodes do have a lineCount property for each speaker and episode. We can use the Aggregate transform to sum those values and write the total to connected Episode nodes.
13Go to the Transform panel and Aggregate tab (take a Snapshot first!).
Under Aggregate to Category, select Episodes.
Under Aggregate Along select SPOKEN_ON.
Click Property from Neighbor Nodes and select lineCount.
For the New Property enter totalLines, select the sum preset, and click Run.
14We can also use Aggregate to return the number of unique characters per Episode.
Select the Episodes category and SPOKEN_ON relationship.
Click Property from Neighbor Nodes, and select the speaker property.
Under New Property enter totalCharacters, under Formula Name select count, and click Run.
15Now display a table to see the new totalLines and totalCharacters properties.
And before we move on, create another Snapshot.
Note that at any time, you can download your snapshots as a single .ZIP archive.
16Now let's simplify the graph using Merge. It combines nodes of a single category or the edges of a single relationship based on a property value.
Go to the Transform panel and Merge tab.
17With Merge we can use the seasonEpisode property to merge Lines nodes for an episode into a single node.
Click Category and select Lines, and select seasonEpisode as the property.
Click Clear Unselected Properties since only the seasonEpisode property is meaningful.
18To simplify the graph we'll use Merge to combine the lines nodes for an episode into a single node, using the seasonEpisode property.
Click Category and select Lines, and select seasonEpisode as the key property. Click Clear Unselected Properties since only the seasonEpisode property will be meaningful.
19Now click Run. With only one Lines node per Episode, we can now visualize the content of Episodes more clearly.
But we can simplify the graph even further. In Module 6. Shortcut, we'll return to the Snapshot we created before merging to see how.