Skip to content

Transforming the Datasets

veerleprins edited this page Nov 11, 2020 · 2 revisions

To create a visualization, it is useful to form the cleaned data into an array that can be used. For this I wrote several functions that transform the cleaned dataset into an array that can be used for a visualization.

First I started to merge the two datasets into one dataset. I did this by linking the areaids together. When they matched, I added certain values to a new array. My code is shown below:

// Merging the two datasets together:
export function mergeData(dataset1, dataset2) {
  return dataset2.map((item) => {
    const specs = dataset1.find((obj) => item.areaid === obj.areaid);
    if (specs !== undefined) {
      item.chargingCapacity = specs.chargingpointcapacity;
      item.parkingCapacity = specs.capacity;
    } else {
      item.chargingCapacity = 0;
      item.parkingCapacity = 0;
    }
    return item;
  })
}

I only found out later that this code is not really functionally written. Suppose I later want to use other datasets, I would not be able to use this code anymore. For this reason I have adjusted my code. This is shown below:

// This function merges two datasets together:
export function mergeData(dataset1, dataset2, itemArr) {
  return dataset2.map(obj1 => {
    const filtered = dataset1.find(obj2 => obj1[itemArr[0]] === obj2[itemArr[0]]);
    obj1[itemArr[1]] = filtered;
    return obj1;
  });
}

In the parameters I still include the two datasets, but now I also include an itemArr in which I indicate which value should be compared and under which name the same values should appear in the first dataset.

After this I found out that there was quite a bit of location data with no specifications. This is because in some parking locations nothing is known about the specifications (no corresponding areaid). Because of this I wrote a short function that adds a dummy object where each value contains a NaN. I did this with the code below:

// This function transforms the data that has no data available:
export function createNaN(dataArray) {
  dataArray.forEach(item => {
    item.specifications == undefined ? item.specifications = {
      capacity: NaN,
      chargingpointcapacity : NaN
    } : item.specifications
  })
}

As a final step, I only retrieved the values from the entire dataset that I needed for visualizations. In this function I return an array of objects with the specific values. This function is shown below:

// This function creates a new array with only the specific data:
export function createArr(data) {
  return data.map(element => {
    return {
      description : element.areadesc,
      capacity : element.specifications.capacity,
      location : element.location,
      chargingPoints :  element.specifications.chargingpointcapacity
    };
  });
}
Clone this wiki locally