Basic Introduction To R
VICTOR OMONDI
Posted on May 6, 2020
In this article we will cover the intro basics to R Programming Language. This article is meant for people willing to start programming in R, beginners in R who are willing to expand their already gained knowledge in R, or people willing to start their Data Science journey using R programming language. If you are familiar with other programming languages, getting to understand R programming language will be as easy as drinking water.
What is R?
Well! It is a programming language, often used in statistics and data science, but let's get a clear definition from Wikipedia
R is a programming language for statistical computing and data visualization. It has been adopted in the fields of data mining, bioinformatics, and data analysis.
According to Wikipedia: R is a programming language and free software environment for statistical computing and graphics supported by the R Foundation for Statistical Computing. The R language is widely used among statisticians and data miners for developing statistical software and data analysis.
R language provides a wide variety of statistical (linear and nonlinear modelling, classical statistical tests, time-series analysis, classification, clustering, …) and graphical techniques, and is highly extensible. READ MORE>>
How to Install and Getting Started With R
Since this article focuses on the basics of R programming language, for instruction on how to install R and R studio please Check this Edx Article
Once you have R and Rstudio installed, let's proceed to the business of the Day.
Table of Contents
Intro To R Basics
Hello, World!
It's now like a rule that the first code you write when beginning to learn a programming language is a code that will display Hello, World!
on the screen.
In order to display on the screen, R provides the print()
function.
In order to print Hello, World!
in R, you need to pass the string "Hello, World!"
in the print()
function. the strings can be in double quotes ""
or single quotes ''
.
print("Hello, World!")
the output will be as follows:
[1] "Hello, World!"
Creating and Naming Variables
Variables helps your program claim a piece of memory when it's running. In R, variables allow you to store a value or an object. In simple terms, a variable is a named storage space. The variables created can later on be used in your program to perform various operations.
In R, <-
, =
or ->
are used as the Assignment Operator to assign values to a variable name. Before creating variables, you must adhere to the rules of creating a Variable in R.
Rules for Naming Variables in R
-
Variable names MUST start with a letter, it can contain a letter, number, underscore (
_
) and period (.
).unlike other languages, Underscore(
_
) at the beginning of the variable name are NOT allowed in R Keywords CANNOT be used to name variables
Special Character like
#
, or&
and whitespace eg tab or space CANNOT be used in variable namesVariable names are Case Sensitive, therefore,
my_name
is different frommy_Name
.
Below are examples of correct variable names and assignment in R
age <- 17
age
[1] 17
name.first = "Victor"
name.first
[1] "Victor"
17347.3847 -> weight_kgs
weight_kgs
[1] 17347.38
R Arithmetic Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. R Arithmetic Operators are symbols that tells the compiler which mathematical operation to perform. R language supports the following arithmetic operators:
Arithmetic Operator | Function | Description | Example | Output |
---|---|---|---|---|
+ |
Addition | adds a number to another number | 3+2 |
[1] 5 |
- |
Subtraction | subtracts a number from another number | 3 - 2 |
[1] 1 |
* |
Multiplication | multiplies number with another number | 3 * 2 |
[1] 6 |
/ |
Division | splits a number into equal parts | 3/2 |
[1] 1.5 |
^ |
Exponentiation | raising a number to the power of another number | 3 ** 2 |
[1] 9 |
%% |
Modulo | The modulo returns the remainder of the division of the number to the left by the number on its right | 3 %% 2 |
[1] 1 |
Let us use R arithmetic operators to calculate my age. We will use the already gained knowledge of variables and arithmetic operations we've discussed above.
year_of_birth = 2001
current_year = 2020
age = current_year - year_of_birt
age
[1] 19
As you can see age
is a variable that stores the computations done when subtracting year_of_birth
from current_year
.
Comments
R makes use of the #
sign to add comments. Comments are added to a source code so that you and others can understand what the R code is about. Comments are not run as R code, so they will not influence your programs computation computations.
# My year of Birth
year_of_birth = 2001
# The Current Year
current_year = 2020
# Calculating my age by subtracting year_of_birth from the current_year
age = current_year - year_of_birth
age
[1] 19
Basic Data Types in R
There are numerous data types in R programming language, we will cover the 4
basic types, that is, numerics, integers, logical and characters
Data type is the classification/categorization of data item. Everything in python is an object therefore, data types are classes and variables are the instance (object) of the data type.
Useclass(variable_name)
in order to understand to which data type a variable belongs.
To get you started with R Basic Data types, note the following:
- Decimal values are called numerics
- Natural Numbers are called Integers, Integers are also numerics
- Boolean values, eg(
FALSE
orTRUE
) are called logical. R is case sensitive, logicals must be in UPPERCASE- String values/text values are called characters
In the following code, we will create variables then use class()
to check to which data type they belong.
my_age = 14
class(my_age)
my_weight = 90.6
class(my_weight)
my_name = "Victor" #Note how the quotation marks on the right indicate that "Victor" is a character
class(my_name)
is_teenager = TRUE
class(is_teanager)
[1] "numeric"
[1] "numeric"
[1] "character"
[1] "logical"
More Resources
Programs
And that marks the end of this article, keep an eye for more R articles as I cover more of R programming language to get you started in Data Science with R. Have any question? Ask Me on Twitter:
Posted on May 6, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.