Icon

03 Loops and Switches - Solution

03 Loops and Switches - Exercise (Solution)

This workflow shows a solution to a hands-on exercise in the L2-DW Introduction to KNIME Analytics Platform for Data Wranglers - Advanced course

Task 1: Build a Group LoopYour task is to build a group loop that compares the life expectancy in each country to the average value on the continent where it'slocated.1. Read the JoinedCountryTable.table file by executing the top Table Reader node. This table contains information about thedemographics, geolocation, and travel risks in the different countries/territories in 2020.2. Start a group loop that handles each continent in a separate iteration3. Build the loop body:- Calculate the average overall life expectancy on the continent- Transform the value into a flow variable- Assign each country the value "below" if their overall life expectancy is below the average and the value "above" otherwise. Use theRule Engine node and append a new column. 4. Close the loop by concatenating the rows from the different iterations Task 2: Build a Counting LoopYour task it to build a counting loop that forecasts the population in the next five years.1. Execute the provided workflow2. Start a counting loop with 5 iterations on the top output of the Column Splitter node3. Forecast the population in 2020 by adding the value in the Change column to the value in the Population (2019/07/01) column oncein the first iteration. Forecast the population value in 2021 by adding it two times in the second iteration, and so on. Use the MathFormula node and append a new column called "Forecast".4. Name the Forecast according to the year: - Add the value of the currentIteration flow variable to 2020. Use the Math Formula (Variable) node.- Create a column name which contains the value of this flow variable, for example: "Forecasted population in 2020". Use the StringManipulation (Variable) node.- Rename the Forecast column5. Exclude all other columns but the newly renamed forecast column6. Close the loop by joining the columns from the different iterations7. Join the country names from the bottom output of the Column Splitter node to the loop output Task 3: Execute an IF SwitchThe provided workflow visualizes life expectancy values by regions in a bar chart and by countries in achoropleth world map. Your task is to build an IF switch that displays one of these alternative views.1. Create a Single Selection Configuration node with two options: "Regions" and "Countries"2. Encode the output of the configuration node into "top" and "bottom". Use the Rule Engine Variablenode.3. Use a switch node to separate the two workflow branches. Control the active port by the flowvariable that you created in the previous step.4. Encapsulate the nodes into a component ReadJoinedCountryData.tableOne iterationper continentavg life expectancyby continentbelow or above thecontinent averageAdd change to currentpopulationColumn header2020 + current iterationRename column withforecasted valuesOnly forecastedvaluesRead population2019.tableNode 28country namesat bottominner join on rowIDconcatenatethe results5 iterationsLifeExpectancy.table Table Reader Group Loop Start GroupBy Rule Engine Math Formula String Manipulation(Variable) Math Formula(Variable) Column Rename Loop End (ColumnAppend) Column Filter Table Rowto Variable Table Reader Component Column Splitter Joiner Loop End Counting Loop Start Table Reader Task 1: Build a Group LoopYour task is to build a group loop that compares the life expectancy in each country to the average value on the continent where it'slocated.1. Read the JoinedCountryTable.table file by executing the top Table Reader node. This table contains information about thedemographics, geolocation, and travel risks in the different countries/territories in 2020.2. Start a group loop that handles each continent in a separate iteration3. Build the loop body:- Calculate the average overall life expectancy on the continent- Transform the value into a flow variable- Assign each country the value "below" if their overall life expectancy is below the average and the value "above" otherwise. Use theRule Engine node and append a new column. 4. Close the loop by concatenating the rows from the different iterations Task 2: Build a Counting LoopYour task it to build a counting loop that forecasts the population in the next five years.1. Execute the provided workflow2. Start a counting loop with 5 iterations on the top output of the Column Splitter node3. Forecast the population in 2020 by adding the value in the Change column to the value in the Population (2019/07/01) column oncein the first iteration. Forecast the population value in 2021 by adding it two times in the second iteration, and so on. Use the MathFormula node and append a new column called "Forecast".4. Name the Forecast according to the year: - Add the value of the currentIteration flow variable to 2020. Use the Math Formula (Variable) node.- Create a column name which contains the value of this flow variable, for example: "Forecasted population in 2020". Use the StringManipulation (Variable) node.- Rename the Forecast column5. Exclude all other columns but the newly renamed forecast column6. Close the loop by joining the columns from the different iterations7. Join the country names from the bottom output of the Column Splitter node to the loop output Task 3: Execute an IF SwitchThe provided workflow visualizes life expectancy values by regions in a bar chart and by countries in achoropleth world map. Your task is to build an IF switch that displays one of these alternative views.1. Create a Single Selection Configuration node with two options: "Regions" and "Countries"2. Encode the output of the configuration node into "top" and "bottom". Use the Rule Engine Variablenode.3. Use a switch node to separate the two workflow branches. Control the active port by the flowvariable that you created in the previous step.4. Encapsulate the nodes into a component ReadJoinedCountryData.tableOne iterationper continentavg life expectancyby continentbelow or above thecontinent averageAdd change to currentpopulationColumn header2020 + current iterationRename column withforecasted valuesOnly forecastedvaluesRead population2019.tableNode 28country namesat bottominner join on rowIDconcatenatethe results5 iterationsLifeExpectancy.table Table Reader Group Loop Start GroupBy Rule Engine Math Formula String Manipulation(Variable) Math Formula(Variable) Column Rename Loop End (ColumnAppend) Column Filter Table Rowto Variable Table Reader Component Column Splitter Joiner Loop End Counting Loop Start Table Reader

Nodes

Extensions

Links