The Basics - Tasks

We are going to start from scratch and build up some basic skills in R. Follow the steps below to get started.

Tip

Get R and your IDE (RStudio, VS Code, or Positron) running (see Prerequisites if needed) before starting.

Setting up your first(?) project:

  1. In your IDE (RStudio example shown), go to File > New Project....

    • Select New Directory.
    • Select New Project.
    • Name the project DS-intro-2025 and choose a directory to save it in (your OneDrive might be a good choice).
    • Click Create Project.

    Note: The exact steps may vary slightly in VS Code or Positron, but the concept of project organization remains the same.

  2. Navigate to the Files tab, and create two folders in the project directory: data and scripts.

Now using the console only:

  1. Type: print("Hello, world!") and hit Enter to see the output.
  2. Now try: 2 + 2 and hit Enter.
  3. Find what the current working directory is by typing getwd() and hitting Enter.
  4. Assign the value 10 to a variable named my_number using the assignment operator <- or =.
  5. Print the value of my_number to the console. Did it work?

Now, let’s create your first R script: 8. In RStudio, go to File > New File > R Script.

Note

R scripts allow you to write and save your code. You can run the code directly from the script, and it helps you keep your work organized.

Think of the script as a recipe that you can follow again and again! If you want to repeat your analysis or share it with others, having it in a script makes it easy.

All lines in the script that start with # are comments. They are not executed as code but are there to help explain what the code does.

  1. Save the script in the scripts folder as 1-basics-intro.R.

  2. Type the following code in the new script:

    # This is my first R script
    print("Hello, world!")
    my_number <- 10
    print(my_number)
  3. Run the code in the script by placing your cursor on each line and pressing Ctrl + Enter (or Cmd + Enter on Mac). Observe the output in the console.

  4. Create a vector with 1000 random numbers between 0 and 100. For this, you can use the runif() function. Assign this vector to a variable named random_numbers. Check how to specify the minimum and maximum values in the runif() function by typing ?runif in the console.

  5. Install the tidyverse package by typing install.packages("tidyverse") in the console. We won’t use it now, we will load it in the next part.

Optional:

  1. Calculate the mean and standard deviation of the random_numbers vector using the mean() and sd() functions. Print the results to the console.
  2. Save your script by clicking File > Save or pressing Ctrl + S (or Cmd + S on Mac).

Reuse