Icon

03 Loops and Switches

03 Loops and Switches - Exercise

This workflow shows 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's located.1. Read the JoinedCountryTable.table file by executing the top Table Reader node. This table contains information about the demographics, geolocation, and travel risks in the differentcountries/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 the Rule 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 once in the first iteration. Forecast the populationvalue in 2021 by adding it two times in the second iteration, and so on. Use the Math Formula 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 String Manipulation (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 a choropleth world map. Your task isto 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 Variable node.3. Use a switch node to separate the two workflow branches. Control the active port by the flow variable that you created in the previousstep.4. Encapsulate the nodes into a component ReadJoinedCountryData.tableNode 48Node 47Node 45Node 49Node 50Read population2019.tableNode 43Node 51Node 44LifeExpectancy.tableNode 38Node 39Node 40Node 41Node 42Table Reader Math Formula String Manipulation(Variable) Math Formula(Variable) Column Rename Loop End (ColumnAppend) Column Filter Table Reader Column Splitter Joiner Counting Loop Start Table Reader Group Loop Start GroupBy Table Rowto Variable Rule Engine Loop End Component 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's located.1. Read the JoinedCountryTable.table file by executing the top Table Reader node. This table contains information about the demographics, geolocation, and travel risks in the differentcountries/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 the Rule 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 once in the first iteration. Forecast the populationvalue in 2021 by adding it two times in the second iteration, and so on. Use the Math Formula 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 String Manipulation (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 a choropleth world map. Your task isto 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 Variable node.3. Use a switch node to separate the two workflow branches. Control the active port by the flow variable that you created in the previousstep.4. Encapsulate the nodes into a component ReadJoinedCountryData.tableNode 48Node 47Node 45Node 49Node 50Read population2019.tableNode 43Node 51Node 44LifeExpectancy.tableNode 38Node 39Node 40Node 41Node 42Table Reader Math Formula String Manipulation(Variable) Math Formula(Variable) Column Rename Loop End (ColumnAppend) Column Filter Table Reader Column Splitter Joiner Counting Loop Start Table Reader Group Loop Start GroupBy Table Rowto Variable Rule Engine Loop End Component

Nodes

Extensions

Links