R Language: The Ultimate Beginner Tutorial

Part 1:

R is a programming language, that is often associated with statistical computing, used for graphical presentation and analyzing and visualizing data.

R language was developed by Ross Ihaka and Robert Gentleman, you probably notice that both their first names start with R ūüėÄ. The two were both statisticians at the University of Auckland, New Zealand.

That said, R comes with the full power of a programming language and should be able to execute tasks required of programming languages.

  • The language is so easy to get started.
  • It is easy to draw graphs and for the basic ones, no libraries are required.
  • It is a great resource for data analysis, data visualization, data science, and machine learning.
  • It has large community support.
  • Many packages provided can be used to solve various problems.
  1. One should know how to install software
  2. No other special knowledge or skill is required to follow along with this tutorial
  • To get started, let's first download and install the latest version of R, follow this link¬†and download the latest version for your operating system

  • When you are done downloading and installing R, you can run R in your computer's command prompt. This is how

  • Press the Windows button
  • Write cmd and press enter

In the window that opens, write R and press enter, you should get the result as below(on windows)

Screenshot (84).png

We shall use this to practice what is in this tutorial.

R Lang has a unique way of printing out items, for example, this is how you can print out hello world

"Hello world"

For me that looked a funny way because I had a background in python and javascript, luckily, R also provided a print() function which comforted me. 

print("Hello world")

I prefer to use this, but it's purely personal preference and you do not have to use it.

Comments are statements used to explain code. In R, we use a # to comment

# This is a comment
"This is the code"

You can use commenting to prevent the execution of a certain code when testing one code

  • In programming, we use variables to store values for later use.
  • In R, a variable is created at once when we assign a value to it.
  • To assign a value to a variable, we use the <- sign. It is possible to use the = sign, but we'll stick to the former in this tutorial.¬†
name <- "Juma"
age <- 22

# Print out the variables
name    # or print(name)
age     # or print(age)

From the above, example, you may be interested in printing out both variables together. To do this, we will use the paste() function and we will use commas to combine them. This is how

name <- "Juma"
age <- 22

paste(name, age)

Creating variables in R is easy, but there are some guidelines to follow, or you could easily make mistakes that will break your code.

  • A variable must start with a letter and can be a combination of letters, digits, and underscore(_)
  • Variable names are case sensitive(eg name and Name are different)
  • Reserved words should not be used as variable names
  • Variables store different forms of data, to differentiate these data, the concept of data types comes in.
  • In R lang, we have 5 major data types:

    1. Numeric
    2. Integer
    3. complex
    4. character and Strings
    5. logical
  • You can check the data type of a number by using the¬†class()¬†function.

These contain any numbers with or without a decimal, for example, 1, 5.5, or 50. Example

num1 <- 5.5
num2 <- 4.5

# print out our numbers
num1     # equivalent to print(num1) 

# print the class names of the variables
class(num1)      #Equivalent to print(class(num1))

num1 and num2 are just some random variables to represent the numbers.

  • These are numeric data¬†without decimals. You can use these when you are cock sure you will never create variables that should contain decimals.
  • To create an¬†integer variable, you must use the letter L after the integer value. Example
int1 <- 100L
int2 <- 33L 

# print the class name of int1 and int2
class(int1)     # equivalent to print(class(int1))

int1 and int2 are just some random variables to represent the integers.

These are numbers written with an "i" as an imaginary part. For example

comp1 <- 2 + 7i
comp2 <- 3i

# Check for the data type
class(comp1)    #equivalent to print(class(comp1))

You can convert variables from one type to another. This can be done using the following functions.

  • as.numeric()
  • as.integer()
  • as.complex()

Examples below can show how this is done

int1 <- 4L  #integer
num1 <- 2   # numeric

# convert from integer to numeric
new_num <- as.numeric(int1)

#convert from numeric to integer
new_int <- as.integer(num2)

Now if you test our new variables using the class() function, you'll notice that their data types have changed.

In programming, we use strings and characters for storing texts. A string is surrounded by either single quotation marks or double quotation marks. Example

"Hello Shafara"
'Hello Shafara'

Now can be wondering, when do I use which, you can use any pair that you like. Just make sure if you're using single, open and close with single, and likewise with double.

Strings too can be stored in variables. Example

str <- "Shafara loves coding"

# print the string out
str     # Equivalent to print(str)
  • In all aspects of life, you'd like to know if something is true or false, likewise in programming ūüėĀ.
  • You can evaluate any expression in R and get one of the answers,¬†TRUE¬†FALSE.
  • When you compare two values, the expression is evaluated and R returns the logical answer.

Example 1:

1 > 9  # FALSE
5 > 3  # TRUE

We could also use conditions in if statements. Example 2:

first_num <- 5
second_num <- 7

if(first_num > second_num){
    print("First number is greater than second number")
    print("First number is not greater than second number")

We learn about if statements in the second part of this article.

  • In any programming language, we use operators to perform operations on variables and values.
  • In this example below, we are adding two values using the¬†+ operator.
1 + 6

In R, operators are grouped into 5 major groups

  1. Arithmetic operators
  2. Assignment operators
  3. Comparison operators
  4. Logical operators
  5. Miscellaneous operators

In R, we use arithmetic operators with numeric values to perform common mathematical operations.

Operator Name Example
+ Addition x + y
- Subtraction x - y
* Multiplication x * y
/ Division x / y

In R lang, we use assignment operators to assign values to variables. For example

my_var <- 3

3 -> my_var

my_var = 3

Comparison operators are used to compare two values

Operator Name Example
== Equal x == y
!= Not equal x != y
> Greater than x > y
< Less than x < y
>= Greater than or equal to x >= y
<= Less than or equal x <= y

Logical operators are used in combining conditional statements

Operator Description
& Element-wise Logical AND operator. It returns TRUE if both elements are TRUE
&& Logical AND operator. Returns TRUE if both statements are TRUE
I Element-wise logical OR operator. Returns TRUE id one of the statements is TRUE
II Logical OR operator. Returns true if one of the statements is TRUE
! Logical NOT. Returns FALSE if statement is TRUE

We look at Miscellaneous operators later in Part 2 of this article.

This is where the first part of this article ends, I hope it has gotten you kicking, sometimes programming can be disturbing when starting but later concepts become clear and patterns are found.

If you found this article helpful, leave a comment on the interesting things, comment about the disturbing concepts.

WRITTEN by Kibekityo Juma Shafara(Applied Data Scientist)

Leave a Reply Cancel reply