Kibekityo Juma Shafara
Kibekityo Juma Shafara's Blog

Follow

Kibekityo Juma Shafara's Blog

Follow
The R Language

Photo by Brannon Naito on Unsplash

The R Language

The Ultimate Beginner Tutorial

Kibekityo Juma Shafara's photo
Kibekityo Juma Shafara
ยทJun 11, 2022ยท

7 min read

Table of contents

Part 1:

Introduction

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.

why use R Language

  • 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.

Prerequisites

  1. One should know how to install software
  2. No other special knowledge or skill is required to follow along with this tutorial

Get Started with R Lang

  • To get started, lets 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.

Printing out

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. Ie

print("Hello world")

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

Commenting in R

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

# This is a comment
"This is the code"

Tip

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

Variables

  • 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. Ie
name <- "Juma"
age <- 22

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

Concatenating items

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)

Naming variables

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

R Data Types

  • 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.

1. Numeric

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) 
num2

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

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

2. Integer

  • 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.

3. Complex

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))
class(comp2)

Type conversion

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.

4. Strings

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)

5. Logical/Booleans

  • 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")
}else{
    print("First number is not greater than second number")
}

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

Operators

  • 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

1. Arithmetic operators

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

OperatorNameExample
+Additionx + y
-Subtractionx - y
*Multiplicationx * y
/Divisionx / y

2. Assignment Operators

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

my_var <- 3

3 -> my_var

my_var = 3

3. Comparison Operators

Comparison operators are used to compare two values

OperatorNameExample
==Equalx == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater than or equal tox >= y
<=Less than or equalx <= y

4. Logical Operators

Logical operators are used in combining conditional statements

OperatorDescription
&Element-wise Logical AND operator. It returns TRUE if both elements are TRUE
&&Logical AND operator. Returns TRUE if both statements are TRUE
IElement-wise logical OR operator. Returns TRUE id one of the statements is TRUE
IILogical 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.

You can follow on to part 2 here.

Follow me so you don't miss out on up coming parts.

ย 
Share this