Hamburger Icon
  • Labs icon Lab
  • Data
Labs

Building Your First R Analytics Solution Hands-on Practice

This lab, titled "Building Your First R 3 Analytics Solution," is designed to equip learners with the foundational skills needed for analytics projects in R. It starts by teaching how to work with R functions and write reproducible scripts, ensuring analyses are transparent and repeatable. The lab emphasizes the importance of organization through RStudio projects and introduces R Markdown as a powerful tool for documenting and communicating analysis findings, making it an essential primer for anyone looking to harness R for data analysis projects.

Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 40m
Published
Clock icon Feb 27, 2024

Contact sales

By filling out this form and clicking submit, you acknowledge ourΒ privacy policy.

Table of Contents

  1. Challenge

    Working with R Functions and Writing Reproducible R Scripts

    RStudio Guide

    To get started, click on the 'workspace' folder in the bottom right pane of RStudio. Click on the file entitled "Step 1...". You may want to drag the console pane to be smaller so that you have more room to work. You'll complete each task for Step 1 in that R Markdown file. Remember, you must run the cells with the play button at the top right of each cell for a task before moving onto the next task in the R Markdown file. Continue until you have completed all tasks in this step. Then when you are ready to move onto the next step, you'll come back and click on the file for the next step until you have completed all tasks in all steps of the lab.


    Working with R Functions and Writing Reproducible R Scripts

    To review the concepts covered in this step, please refer to the Working with R Code in RStudio module of the Building Your First R Analytics Solution course.

    Understanding how to work with R functions and write reproducible R scripts is important because it allows for efficient data analysis and ensures that your work can be replicated by others. This step will help you practice calling R functions, passing arguments to functions, and writing reproducible R scripts.

    In this step, you'll dive deeper into the world of R programming. First, you'll practice loading R packages into the environment. You'll practice calling R functions, which are predefined pieces of code that perform a specific task. You'll also practice passing arguments to functions, which are the inputs that the function uses to perform its task. Finally, you'll practice writing reproducible R scripts, which are scripts that can be run by others to reproduce your analysis. The goal is to improve your R programming skills and learn to write code that others can easily understand and replicate.


    Task 1.1: Understanding R Packages and Functions

    Load the ggplot2 library (it is already installed). Use the qplot function to make a scatterplot.
    Use the provided x and y variables, which correspond to the first two arguments of qplot.

    πŸ” Hint

    Use the library() function to load ggplot2. Then call the qplot() function using x and y as arguments.

    πŸ”‘ Solution
    library(ggplot2)
    qplot(x, y)
    

    Task 1.2: Call a Function with Named Arguments

    The qplot() function has a named argument main that allows you to add a main title to your plot. Add main as a named argument and give your plot the title 'My Plot'

    πŸ” Hint

    To use a named argument, use the syntax function(..., argument = value). In this case, the argument is main and the value is 'My Plot'.

    πŸ”‘ Solution
    qplot(x, y, main = 'My Plot')
    

    Task 1.4: Writing a Reproducible R Script

    Examine the provided R script. This script is written in a way that is not reproducible, If you run it, you will encounter an error. Fix the script so that it becomes reproducible.

    πŸ” Hint

    There are two sections of the R script that are out of order. Ensure that any R packages are loaded before using functions from those packages. Also ensure that any new variables are created before they are referenced.

    πŸ”‘ Solution
    library(readr)
    student_scores <- read_csv('student_scores.csv')
    subjects <- c('math_score', 'english_score', 'science_score')
    colMeans(student_scores[subjects])
    
  2. Challenge

    Organizing Your Work with RStudio Projects

    Organizing Your Work with RStudio Projects

    To review the concepts covered in this step, please refer to the Organizing Your Work with RStudio Projects module of the Building Your First R 3 Analytics Solution course.

    Knowing how to organize your work with RStudio projects is important because it helps keep your work organized and makes it easier to share with others. This step will help you practice creating RStudio projects, adding files to projects, and referring to files in R scripts.

    In this step, you'll learn to keep your work organized with RStudio projects. You'll practice creating RStudio projects, which are a way of keeping related files and settings together in one place. You'll also practice adding files to projects and referring to these files in your R scripts. The goal is to improve your project management skills and learn to work more efficiently in RStudio. You'll be using the getwd, dir, and read_csv functions.


    Task 2.1: Create a new RStudio project

    Create a new RStudio project in the workspace directory.

    πŸ” Hint

    Start by navigating to File -> New Project in the the RStudio menu.

    πŸ”‘ Solution

    File -> New Project -> Existing Directory -> Browse
    Choose the workspace directory


    Task 2.3: Check the current working directory

    When a project is open, your working directory will be set to the directory of the project.

    In the Console window, check the current working directory to make sure it's the directory of your RStudio project.

    πŸ” Hint

    Use the getwd function to check the current working directory.

    πŸ”‘ Solution
    getwd()
    

    Task 2.4: List the files in your project directory

    List the files in your project directory to make sure the data file is there.

    πŸ” Hint

    Use the dir function to list the files in your project directory.

    πŸ”‘ Solution
    dir()
    

    Task 2.5: Use the Console to read files in the project directory

    In the Console, load the readr package. Then, use the read_csv() function to load the data into the R environment.

    πŸ” Hint

    Use the read_csv() function to read the data file into a data frame. Make sure to use the correct file name.

    πŸ”‘ Solution
    library(readr)
    data <- read_csv('student_scores.csv')
    
  3. Challenge

    Communicating and Documenting Your Analysis with R Markdown

    Communicating and Documenting Your Analysis with R Markdown

    To review the concepts covered in this step, please refer to the Communicating and Documenting Your Analysis with R Markdown module of the Building Your First R 3 Analytics Solution course.

    Being able to communicate and document your analysis with R Markdown is important because it allows you to share your findings in a clear and reproducible way. This step will help you practice creating R Markdown documents, working with code and text in these documents, and controlling the appearance of the output.

    In this step, you'll learn to communicate and document your analysis with R Markdown. You'll practice creating R Markdown documents, which are a way of interweaving code, results, and text in a single document. You'll also practice working with code and text in these documents, and controlling the appearance of the output. The goal is to improve your communication skills and learn to create reproducible reports.


    Task 3.1: Add a code chunk to your R Markdown file

    Add a code chunk. Name the code chunk setup and change the options so that the chunk will be run, but neither the code nor its output will appear in the document.

    πŸ” Hint

    The syntax for a code chunk starts with three backticks followed by open curly braces {}. Within the curly braces, include r, the name of your code chunk, and any relevant options.

    The include option controls whether the code chunk and its output appears in the document. Close the code chunk with another three backticks.

    πŸ”‘ Solution
    ```{r setup, include=FALSE}
    
    ```
    

    Task 3.2: Load the data into your R Markdown file

    Within your code block, load the student scores data into your R Markdown file using the read_csv() function from the readr package. Save the data to a variable named data.

    πŸ” Hint

    Use the library() function to load the readr package. Then call the read_csv() function with the data file name as the argument.

    πŸ”‘ Solution
    library(readr)
    data <- read_csv('student_scores.csv')
    

    Task 3.3: Add a code chunk that is visible in the document

    Create a new code chunk named calculate_average. Set the options so that the code will appear in the document. Within this code chunk, calculate the average of the age column and save it to a variable named average_age.

    πŸ” Hint

    The echo option can be used to control whether code appears in the output. Use the mean() function to calculate an average.

    πŸ”‘ Solution
    ```{r calculate_average, echo=TRUE}
    average_age <- mean(data$age)
    ```
    

    Task 3.4: Add text to your R Markdown file

    Below the code chunk, write the text "The average age in the sample is ___". In the blank space, use inline R code to print the average age, rounded to 1 decimal place.

    πŸ” Hint

    You can include the results of code in your text by using the r syntax.

    πŸ”‘ Solution

    The average age in the sample is `r round(average_age, 1)`.


    Task 3.5: Knit your R Markdown file

    Knit your R Markdown file to an HTML document.

    πŸ” Hint

    Use the RStudio Knit menu, located below the open tabs, to knit the document to HTML.

    πŸ”‘ Solution

    Knit -> Knit to HTML

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.