Category: How to

  • 19. How to visualize data with RStudio (R programming language)

    #RStudio #R #R_Programming_language

    RStudio is an integrated development environment that allows you to create and manage projects using R language. R is a programming language for statistical computing and data visualization.

    Sign up for an account at the RStudio Cloud in https://posit.cloud/

    Create a New Project

    This will open the RStudio Cloud console.

    Then you need to install the packages to RStudio Cloud. Packages are units of reproducible R code.

    First install tidyverse: the tidyverse is a collection of packages in R for data manipulation, exploration, and visualization. To install the core tidyverse packages and load them: in the bottom of the console, type install.packages(“tidyverse”) and press Enter.

    To load the core tidyverse, type library(tidyverse) and press Enter

    Many of the `tidyverse` packages contain sample datasets to practice `R` skills, e.g.  the `diamonds` dataset (already loaded together with tidyverse package).

    To preview the data use the `head()` function, which displays the columns and the first several rows of data.

    Other useful functions to summarize or preview the data:

    • str() and glimpse() functions will both return summaries of each column in your data arranged horizontally.
    • the colnames() function returns a list of column names from your dataset
    • the summarize() function generates a wide range of summary statistics for your data. For example, to know what the mean for `carat` was in this dataset use summarize(diamonds, mean_carat = mean(carat))

    To clean data:

    • the function to rename the columns, or variables, in your data is rename(), e.g.rename(diamonds, carat_new = carat, cut_new = cut)

    To visualize data:

    The most commonly used visualization packages is ggplot2, automatically loaded together tidyverse.

    The code that takes the `diamonds` data, plots the carat column on the X-axis, the price column on the Y-axis, and represents the data as a scatter plot using the `geom_point()` command and changes the color of each point so that it represented another variable, such as the cut of the diamond is:

    To create a different plot for each type of cut use the `facet_wrap()` function:

  • 18. How to start visualizing data with Tableau

    #dataviz #Tableau

    Create a Tableau public profile in https://public.tableau.com/app/discover

    Click on your profile to create a new visualization.

    Click on Create a visualization.

    Tableau opens a window to Connect data.

    You can find sample data here -> https://public.tableau.com/app/learn/sample-data

    e.g. Download the Excel file with data for the Significant volcanic eruptions from (source https://www.ncei.noaa.gov/)

    Upload the data file in Tableau.

    Drag and drop the table to create a data model.

    When uploaded click on Update now to populate the table.

    Click on Sheet 1 (bottom left) to create a visualization.

    Double click or drag and drop the fields in the left Data pane to add data for the visualization.

    Selecting Country the canvas displays a map of the countries with points indicating which countries are represented in the data.

    The points are all the same size because, if there is no measure selected, Tableau defaults to scale each country equally.

    Double clicking on a measure (e.g. Volcano explosivity) the dot sizes change.

  • 17. How to use conditional functions COUNTIF, SUMIF, AVERAGEIF, and MAXIFS in Excel and Google Sheets

    #conditonalfunctions #COUNTIF #SUMIF #AVERAGEIF #MAXIFS #Excel #GoogleSheets

    Conditional functions perform a specific task, but only on cells that satisfy some defined criteria. They are frequently used when constructing more complex queries.

    Assume you have the following sales dataset in A1:E11

    1️⃣ COUNTIF

    Objective:

    Count how many sales were made in the South region.

    Excel:

    =COUNTIF(C2:C11; “South”)

    Explanation:

    • C2:C11 → range to evaluate
    • “South” → criterion

    This counts all rows where Region = South.

    The same in Google Sheets

    =COUNTIF(C2:C11;”South”)

    2️⃣ SUMIF

    Objective:

    Calculate total revenue generated by Alice.

    =SUMIF(B2:B11;”Alice”;E2:E11)

    Explanation:

    • B2:B11 → criteria range
    • “Alice” → condition
    • E2:E11 → sum range

    This adds revenue only where Salesperson = Alice.

    3️⃣ AVERAGEIF

    Objective:

    Find the average revenue for Laptop sales.

    =AVERAGEIF(D2:D11;”Laptop”;E2:E11)

    Explanation:

    • D2:D11 → criteria range
    • “Laptop” → condition
    • E2:E11 → average range

    Returns the average revenue of all Laptop transactions.

    4️⃣ MAXIFS

    Objective:

    Find the maximum revenue generated in the North region by Alice.

    =MAXIFS(E2:E11;B2:B11;”Alice”;C2:C11;”North”)

    Explanation:

    • E2:E11 → max range
    • B2:B11, “Alice” → first condition
    • C2:C11, “North” → second condition

    Returns the highest single revenue value where both conditions are met = 1300.

  • 16. How to write a SQL subquery in BigQuery

    #SQL #subquery # BigQuery

    A subquery is a query that appears inside another query statement.

    Example: Identify which food industries have the highest number of complaints/recalls from the FDA public database and find out which of these complaints led to hospitalizations.

    The first step is to find which food industries have the highest number of complaints.

    Open BigQuery Console and in your project bigquery-public-data, select the fda_food dataset and the food-events table.

    This is a table preview

    Write a query to find which products-industry-name has the highest number of complaints

    Write a query to find out which of these complaints led to hospitalizations:

    The subquery (in yellow) is a query nested in another query to find which products-industry-name has the highest number of complaints. Among these, the main query selects those that led to hospitalizations. The logical AND operator displays a record if all the conditions are TRUE; the LIKE operator searches for a specific pattern in a column: it returns all products_industry_name in the outcomes column that contain the pattern Hospitalization).

    Reference: https://www.w3schools.com/sql/sql_wildcards.asp

  • 15. How to use SQL Join functions in BigQuery

    #SQL #Join #BigQuery

    Example:

    Import the CSV UTF-8 tables, in this example (CUSTOMERS) and (ORDERS), into BigQuery.

    Add the two tables in a dataset (SALES) of the project (myprojectjoin11022026):

    Write a Query with a LEFT JOIN

    o and c are alias for ORDERS and SALES tables.

    Select the desired fields with a LEFT JOIN to obtain

    A LEFT JOIN means:

    Return all rows from the ORDERS table.

    Include data from CUSTOMERS only if a matching record exists.

    If no match exists → the fields from table c are NULL.

    The key is the Customer_ID. This Left join includes all customer_IDs from the Order table (1, 2, 5) and doesn’t include (null) customer_ID #4 from the Customers table as it is not a match.

  • 14. How to use Pivot tables in Excel

    #PivotTable #Excel

    Pivot tables allow us to organize the data more methodically in complex sheets. Starting from a complex sheet like this:

    After removing all filters, create a Pivot Table in a new Excel sheet from the Insert → PivotTable menu (same command for Goggle Sheets)

    Then select the necessary fields in the top right pane.

    Enlarged view:

    Practical examples of Pivot tables are available in my article Excel, Copilot and Google My Maps for data journalism.

  • 13. How to use VLOOKUP function in Google Sheets

    #VLOOKUP #GoogleSheets

    VLOOKUP is a function that vertically searches for a certain value in a column to return a corresponding piece of information. VLOOKUP helps bring the information together across multiple sheets or even different databases.

    Example:

    Sheet 1 “Statistiche_globali”

    Sheet 2 “Dettaglio_pezzi”

    Task: in the Global_Statistics sheet (sheet 1), retrieve the Cpk of the piece PZ-002 by reading the data from the Piece_Details sheet (sheet 2).

    Write

    =VLOOKUP(“PZ-002”; Dettaglio_Pezzi!A2:E5; 5; FALSE)

    in sheet 1.

    Data to look-up=PZ-002

    Where to look up: Sheet 2 (Dettaglio pezzi)

    ! Range: from cell A2 to cell E5

    Column: 5 (E, CpK)

    False=exact match

  • 12. How to save your query results in a temporary table

    #BigQuery

    Before you run the query, select MORE from the Query Editor and open the Query settings menu.

    This will open:

  • 11. How to Make Sure Your Data Matches Its Description in BigQuery

    #datavalidation #datadescritption #BigQuery

    Based on a hands-on activity of Google Data Analytics Professional issued by Coursera

    File > Download (csv file) the reference table automobile_data

    Create a new data set: open Big Query console, go to the Explorer pane in your workspace to add data

    Create a table (first create a project and a dataset)

    Get an initial understanding of the data table by clicking on the PREVIEW tab

    Enlarged view

    Check the data according to a given data description.

    Requirement 1: the fuel_type column should only have two unique string values: diesel and gas.

    Requirement 2: Length column should range from 141,1 to 208,1.

    Requirement 3: manage null data in the number of doors column.

    Select all lines with a null value in num_of_doors colum with a query

    Update this null value with four (4) with this query:

    Note that Update operation requires a billing account (you can skip). Repeat the same for mazda

    Requirement 4: check potential errors for number of cylinders column

    “tow” in line 7 is a spell-check error. It should be “Two”

    Requirement 5: ensure consistency

    Getting data from a table using SELECT statements.

    De-duplicating data using commands like DISTINCT and COUNT + WHERE.

    Manipulating string data with TRIM to remove extra spaces in a string and SUBSTR to get a portion of a string.

    Creating/dropping tables with CREATE TABLE and DROP TABLE per create or delete tables.

    Changing data types with CAST TO Convert the results of an expression to the given type.