Skip to content

Datacleaning

RobinFrugte97 edited this page Nov 15, 2019 · 10 revisions

Survey data

To show that I am able to clean data, I'm going to clean a row of the CMD survey data. Specifically the row with student hobbys.

I put the row I'm going to clean as a string in my Javascript file.

Raw data Gamen, uitgaan handbal Tekenen; ukelele spelen; schilderen; koken; bakken; haken; films; video's maken; fotografie Sporten; muziek luisteren; series kijken; films kijken; met vrienden uithangen Netflix; Tekenen; Eten voetbal;muziek;vakantie;strand;vrienden bier drinken; muziek luisteren; sporten; Voetbal, Gamen, Design, Schoenen verzamelen Voetbal; Gamen; Bioscoop;

Series kijken; Sporten; Lezen pianospelen; films kijken; simsen; voetballen; tennissen; sporten Honkbal; Auto's; Varen; zwemmen; pianospelen; dansen Muziek maken; muziek produceren; uitgaan met vrienden Skien, surfen, kitesurfen, wielrennen voetbal, gezelligheid, kleding Muziek;Games; Tekenen; gamen(nintendo); lezen; wandelen;

Schilderen en winkelen netflix; festival; shoppen tennis Chillen Lopen Volleybal; graphic design; basketbal Reizen; zwemmen; muziek luisteren vormgeven; gamen; tafeltennis; bioscoop Graffiti, snowboarden; films kijken; koken; reizen Gamen; Lezen rollerskaten;snowboarden;tekenen;tv kijken;drankjes doen Gamen; relaxen;hockey;biertjedrinken Coderen; Tennis; Voetbalwedstrijden analyseren Tekenen, gamen, lezen, schrijven Fotografie; plantenverzorging; slapen; festivalbezoeken Chillen, sporten, uitgaan gamen; leven Gamen; Serie kijken; terrass pakken hockey;netflix;uitgaan voetballen, feesten Netflixen, vrienden meeten, reizen vliegvissen; programmeren; reizen; netflix; dansen; uit gaan; wandelen eten, bios pakken, muziek luisteren, dansen op festivals, rijden Fotografie;DJ Netflix, fitness Netflix; borrelen; slapen muziek; slapen; films; chillen Voetbal gamen; wandelen; fietsen door mooie gebieden; leuke dingen met vrienden; festival bezoeken; koffie drinken; veel planten verzamelen voetballen, boxen, tekenen, dansen, zingen Voetbal;Mountainbiking;Computer-hardware;Handel in technologie;Video-gamen;Meubel-restoraties;Sci-Fi boeken lezen;Japanse cultuur; Tekenen; muziek maken slapen, films kijken Gamen, afspreken met vrienden, uitgaan Wielrennen; Voetballen; Gitaarspelen Voetballen;netflixen:fifa Series kijken ; Muziek luisteren ; Afspreken met mensen ; Festivallen bezoeken ; Lekker eten gamen; netflix kijken; sporten Schilderen; computeren Sporten;computeren Tekenen; Serie's kijken; Drankje drinken Series kijken; Tekenen Films, muziek, games Fitness; Voetbal; Tennis; Uitgaan; Tekenen Muziek luisteren en zoeken Gamen Hockey Drummen; Gamen; Chillen Gamen, karten tekenen; voetballen Zingen; Gitaar spelen; Fotograferen Basketbal;uitgaan skateboarden, fotograferen, filmen, drummen Hobbies zijn voor de delusionals Gitaar spelen; gamen; waterpolo; wandelen; reizen Feesten ; borrelen ; netflixen ; vakantie Motorrijden;Gamen Fitness, Dichten ; blowen Games, tekenen, gitaar, muziek maken Films en series


I started off by creating a main function in which I clean all my data.

I then began splitting the entries at every linebreak;

let splitData = enqueteData.split("\n")

Some people used ; to seperate their entries, others used a ,, a couple others even just used the word "en". I used the map method to go through every entry in the survey, replacing all ,'s with ;'s .map(entry => entry.replace(',', ';')

I then started a method chain by following up the replace with a split to seperate one person's multiple hobby's. I used a regular expression with two cases to split on; .split(/;| en /)


The next thing I wanted to tackle was to capitalize the first letter of each entry. I want to create a seperate function for this, to practice put reusable code in a seperate function. It is also in line with functional programming.

function cleaningData(enqueteData){
    let splitData = enqueteData.split("\n")
        .map(entry => entry.replace(',', ';')
                            .split(/;| en /)
                            .map(hobby => capitalizeFirstLetter(hobby)))
    console.log(splitData);
}

function capitalizeFirstLetter(hobby){
    hobby.charAt(0).toUpperCase()
}

cleaningData(enqueteData)

I tried to add a function that changes every first letter to uppercase, since a lot of entries are all in lowercase. This does not work as it turns all entries into undefined.


I wanted to set all characters to lowercase so I can easily filter out invalid entries and to set the first letter to uppercase.

function entryToLowerCase(splitData) {
    let lowerCaseData = splitData.map(entry => entry.toLowerCase())
    console.log(lowerCaseData);
}

I removed the second split; .split(/;| en /). I did this because this created seperate arrays in the main data array. I wanted to try and make it so the data can be used again in a similar format.

function capitalizeFirstLetter(lowerCaseData){
    let capitalizedData = lowerCaseData.map(entry => entry.charAt(0).toUpperCase())
    console.log(capitalizedData)
}

I continued with capitalizing the first letter of every entry. I tried using the map method to loop through all the entries and the charAt(0) to get the first letter of each entry. I set that letter to uppercase.

When I log the result, I only get the single changed letter in uppercase. I have to join the rest of the entry somehow.

function capitalizeFirstLetter(lowerCaseData){
    let capitalizedLetters = lowerCaseData.map(entry => entry.charAt(0).toUpperCase())
    let joinableData = lowerCaseData.map(entry => entry.slice(1))
    let capitalizedData = []
    capitalizedData.push(capitalizedLetters+joinableData)

    console.log(capitalizedData)
}

I tried pushing the two arrays I want to combine in a single new array. This does not work, as it just pastes the arrays one after another. It doesn't combine them.

function capitalizeFirstLetter(lowerCaseData){
    let capitalizedData = lowerCaseData.map(entry => entry.charAt(0).toUpperCase() + entry.slice(1))
    console.log(capitalizedData)
}

I have to add the rest of the string to it to combine them. If I slice the original entry from the second character, I get the part of the entry I want to combine with the capitalized first letter. (This took me way too long to figure out)


Week 2

Firstly, I made my code a lot more functional by having all the functions return something to the main function, instead of having a waterfall of functions.

let filterTrash = {
    seperators: [";", " ; ", " ;", ","],
    trash: ["hobbies zijn voor de delusionals", "geen", "nvt", "n.v.t", "", " "]
}

Here I create an object with entries I want to remove, or parts of entries I want to replace.

async function cleaningData(data, filterTrash){
    regexBuilder(filterTrash)
    data = await splittingEntries(data)
    data = await replacingCharacters(data, regexps)
    data = await entryToLowerCase(data)
    data = await filterEntries(data, regexps)
    data = await capitalizeFirstLetter(data)
    console.log(data)  
}

This is the main function. The function is asynchronous, and waits for every function to return its outcome before continueing in the next. In the end the result is logged in the console.

function splittingEntries(data){
    return data.split("\n")
}

This function splits on every linebreak. Every line is now inside an array as a seperate entry.

function entryToLowerCase(data) {
    return data.map(entry => entry.toLowerCase())
}

This function remaps the entire array and sets all the entries to lowercase. A new array with lowercase values is returned.

function replacingCharacters(data, regexps){
    return data.map(entry => entry.replace(regexps.seperatorRegex, ', '))
}

This function changes how entries with multiple hobbies are seperated. The seperators that I want to change are located in the filterTrash object. I use this object in the function below to create a regular expression, which I use to replace the seperators with a single consistant seperator.

function regexBuilder(filterTrash) {
    let trashEntryRegex = new RegExp(filterTrash.trash.join("|"), 'gi')
    let seperatorRegex = new RegExp(filterTrash.seperators.join("|"), 'gi')
    return regexps = {trashEntryRegex, seperatorRegex}
}

This function creates regular expressions to use for filtering and replacing entries of parts of entries. It joins items from an array by the | character, which is the regex seperator character. It adds gi to the end to get every occurance in the string I'm changing.

function filterEntries(data, regexps) {
    return data.filter(entry => entry.replace(regexps.trashEntryRegex, 'geen hobby'))
}

This function filters out all worthless entries, as described at the filterTrash object. It uses the regex builder function to build a regular expression to indicate what to replace. I decided to replace these worthless values, which includes empty values, with 'geen hobby'. I think in this case, when a person chooses not to mention their hobby, it can be assumed they have no hobby or just don't want to disclose their hobby. I think 'geen hobby' is a suitable solution.

function capitalizeFirstLetter(data) {
    return data.map(entry => entry.charAt(0)
                .toUpperCase() + entry.slice(1))
}

This function turns the first character of an entry to uppercase. I have to add the rest of the entry back to the capitalized letter otherwise only the first letter will be returned.

Opgeschoonde data ``` "Gamen, uitgaan", "Handbal", "Tekenen, ukelele spelen, schilderen, koken, bakken, haken, films, video's maken, fotografie", "Sporten, muziek luisteren, series kijken, films kijken, met vrienden uithangen", "Netflix, tekenen, eten", "Voetbal, muziek, vakantie, strand, vrienden", "Bier drinken, muziek luisteren, sporten, ", "Voetbal, gamen, design, schoenen verzamelen", "Voetbal, gamen, bioscoop, ", "Series kijken, sporten, lezen", "Pianospelen, films kijken, simsen, voetballen, tennissen, sporten", "Honkbal, auto's, ", "Varen, zwemmen, pianospelen, dansen", "Muziek maken, muziek produceren, uitgaan met vrienden", "Skien, surfen, kitesurfen, wielrennen", "Voetbal, gezelligheid, kleding", "Muziek, games, ", "Tekenen, gamen(nintendo), lezen, wandelen, ", "Schilderen en winkelen", "Netflix, festival, shoppen", "Tennis", "Chillen", "Lopen", "Volleybal, graphic design, basketbal", "Reizen, zwemmen, muziek luisteren", "Vormgeven, gamen, tafeltennis, bioscoop", "Graffiti, snowboarden, films kijken, koken, reizen", "Gamen, lezen", "Rollerskaten, snowboarden, tekenen, tv kijken, drankjes doen", "Gamen, ", "Relaxen, hockey, biertjedrinken", "Coderen, tennis, voetbalwedstrijden analyseren", "Tekenen, gamen, lezen, schrijven", "Fotografie, plantenverzorging, slapen, festivalbezoeken", "Chillen, sporten, uitgaan", "Gamen, leven", "Gamen, serie kijken, terrass pakken", "Hockey, netflix, uitgaan", "Voetballen, feesten", "Netflixen, vrienden meeten, reizen", "Vliegvissen, programmeren, reizen, netflix, dansen, uit gaan, wandelen", "Eten, bios pakken, muziek luisteren, dansen op festivals, rijden", "Fotografie, dj", "Netflix, fitness", "Netflix, borrelen, slapen", "Muziek, slapen, films, chillen", "Voetbal", "Gamen, wandelen, fietsen door mooie gebieden, l…zoeken, koffie drinken, veel planten verzamelen", "Voetballen, boxen, tekenen, dansen, zingen", "Voetbal, mountainbiking, computer-hardware, handel…storaties, sci-fi boeken lezen, japanse cultuur, ", "Tekenen, muziek maken", "Slapen, films kijken", "Gamen, afspreken met vrienden, uitgaan", "Wielrennen, voetballen, gitaarspelen", "Voetballen, netflixen, fifa", "Series kijken, muziek luisteren, afspreken met mensen, festivallen bezoeken, lekker eten", "Gamen, netflix kijken, sporten", "Schilderen, computeren", "Sporten, computeren", "Tekenen, serie's kijken, drankje drinken", "Series kijken, tekenen", "Films, muziek, games", "Fitness, voetbal, tennis, uitgaan, tekenen", "Muziek luisteren en zoeken", "Gamen", "Hockey", "Drummen, gamen, chillen", "Gamen, karten", "Tekenen, voetballen ", "Zingen, gitaar spelen, fotograferen", "Basketbal, uitgaan", "Skateboarden, fotograferen, filmen, drummen", "Gitaar spelen, gamen, waterpolo, wandelen, reizen", "Feesten, borrelen, netflixen, vakantie ", "Motorrijden, gamen", "Fitness, ", "Dichten, blowen", "Games, tekenen, gitaar, muziek maken", "Films en series" ```

Clone this wiki locally