Welcome to the R and RStudio tutorial, part two!
In Getting Started with R and RStudio, Part 1, you became familiar with the RStudio environment, and now you are ready to create your first R file.
Click File on the RStudio menu bar, select New File, and then R Script. Find the floppy-disk icon, as illustrated in the image below, and click on it to save the new file. Now, give it a memorable name, something like “myFirstRFile,” and observe the directory where you are saving this file. Since we set up the “Getting Started with R” folder as a working directory earlier, RStudio should automatically bring you to that folder.
Basic Math with R
After creating a new R file, we can try out various R functionalities by performing basic mathematics and learning along R syntax and procedures.
Let’s go! Navigate to the scripting window, panel 1, and type the code below. The text addition is a comment and is not executed by R since it is preceded by a hash sign, (#.) Those are the author’s comments to help you understand what each line of codes mean.
5+3 # addition
Make sure the cursor is on the code line and press the Run button to execute this line, as illustrated below.
The output should look as follows:
## [1] 8
Tip! Instead of constantly navigating to the Run button, press CTRL+ENTER on Windows or Chromebooks or CMD+ENTER on Mac computers to execute the current line and move the cursor to the following line.
Proceed with practicing more codes as illustrated below and pay attention to the output of each line. Notice that the R language counts each output line, marking it with [number of output line] at the beginning of the line. If the output fits on one line, you will see only [1].
The following operations are examples of simple math functions in R.
5-3 # subtraction
Output: [1] 2
5*3 # multiplication
Output: [1] 15
5/3 # division
Output: [1] 1.666667
5^3 # 5 to the power of 3
Output: [1] 125
Practice more with the codes below and observe how the output changes with various rounding techniques in R. The chances are that you will find those functions beneficial as you grow as a Data Analyst professional!
5/3
Output: [1] 1.666667
ceiling(5/3) # rounds a number upwards to its nearest integer
Output: [1] 2
floor(5/3) # rounds a number downwards to its nearest integer
Output: [1] 1
round(5/3, digits = 2) # rounds a number to the specified decimal places
Output: [1] 1.67
abs(-5) # returns absolute (positive) value of 5
Output: [1] 5
sqrt(5) # returns square root of 5
Output: [1] 2.236068
log(5) # returns natural logarithm of 5
Output: [1] 1.609438
log(5, base = 8) # returns logarithm base 8 of 5
Output: [1] 0.773976
max(5, 3, 1) # returns the highest number in the set
Output: [1] 5
min(5, 3, 1) # returns the lowest number in the set
Output: [1] 1
*Be sure to utilize the Help tab on panel 3 to learn more about each new function you study. Click Help and type the function name into the search box as in the image below.
Need More Help?
Click here to schedule a 1:1 with a tutor, coach, and or sign up for a workshop. *If this link does not bring you directly to our platform, please use our direct link to "Academic Support" from any Brightspace course at the top of the navigation bar.