Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space DEV and version r095
Excerpt

This section describes how to unnest the values in an Array into separate columns in your dataset.

Source:

In the following example dataset, students took the same test three times, and their scores were stored in any array in the Scores column.

LastNameFirstNameScores
AdamsAllen[81,87,83,79]
BurnsBonnie[98,94,92,85]
CannonChris[88,81,85,78]

Transformation:

When the data is imported, you might have to re-type the Scores column as an array:

D trans
RawWrangletrue
Typestep
WrangleTextsettype col: Scores type: 'Array'
p01NameColumns
p01ValueScores
p02NameNew type
p02ValueArray
SearchTermChange column data type

You can now unnest the Scores column data into separate columns:

D trans
RawWrangletrue
p03Value[1]
p06NameRemove elements from original
WrangleTextflatten col: Scores
p01NameColumn
p06Valuetrue
p03NameParameter: Paths to elements
p07Valuetrue
p04Value[2]
SearchTermUnnest Objects into columns
p07NameInclude original column name
Typestep
p05NameParameter: Paths to elements
p01ValueScores
p02NameParameter: Paths to elements
p02Value[0]
p05Value[3]
p04NameParameter: Paths to elements

In the above transformation:

  • Each path is specified in a separate row. 
    • The [x] syntax indicates that the path is the xth element of the array. 
    • The first element of an array is referenced using [0]
  • You can choose to delete the element from the original or not. Deleting the element can be a helpful way of debugging your transformation. If all of the elements are gone, then the transformation is complete. 
  • If you include the original column name in the output column names, you have some contextual information for the outputs.

Results:

LastNameFirstNameScores_0Scores_1Scores_2Scores_3
AdamsAllen81878379
BurnsBonnie98949285
CannonChris88818578

D s also
labelarray