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.
Get R and your IDE (RStudio, VS Code, or Positron) running (see Prerequisites if needed) before starting.
Setting up your first(?) project:
In your IDE (RStudio example shown), go to
File>New Project....- Select
New Directory. - Select
New Project. - Name the project
DS-intro-2025and 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.
- Select
Navigate to the
Filestab, and create two folders in the project directory:dataandscripts.
Now using the console only:
- Type:
print("Hello, world!")and hit Enter to see the output. - Now try:
2 + 2and hit Enter. - Find what the current working directory is by typing
getwd()and hitting Enter. - Assign the value
10to a variable namedmy_numberusing the assignment operator<-or=. - Print the value of
my_numberto the console. Did it work?
Now, let’s create your first R script: 8. In RStudio, go to File > New File > R Script.
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.
Save the script in the
scriptsfolder as1-basics-intro.R.Type the following code in the new script:
# This is my first R script print("Hello, world!") my_number <- 10 print(my_number)Run the code in the script by placing your cursor on each line and pressing
Ctrl + Enter(orCmd + Enteron Mac). Observe the output in the console.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 namedrandom_numbers. Check how to specify the minimum and maximum values in therunif()function by typing?runifin the console.Install the
tidyversepackage by typinginstall.packages("tidyverse")in the console. We won’t use it now, we will load it in the next part.
Optional:
- Calculate the mean and standard deviation of the
random_numbersvector using themean()andsd()functions. Print the results to the console. - Save your script by clicking
File>Saveor pressingCtrl + S(orCmd + Son Mac).