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.
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 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. Polls, data mining surveys, and studies of scholarly literature databases show substantial increases in popularity; as of June 2020, R ranks 9th in the TIOBE index, a measure of popularity of programming languages.
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>>
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.
- What is R
- How to Install and Getting Started With R
- Intro To R Basics
- More Resources
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
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
the output will be as follows:
 "Hello, World!"
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.
-> 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.
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
&and whitespace eg tab or space CANNOT be used in variable names
Variable names are Case Sensitive, therefore,
my_nameis different from
Below are examples of correct variable names and assignment in R
age <- 17 age
name.first = "Victor" name.first
17347.3847 -> weight_kgs weight_kgs
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:
||Addition||adds a number to another number||
||Subtraction||subtracts a number from another number||
||Multiplication||multiplies number with another number||
||Division||splits a number into equal parts||
||Exponentiation||raising a number to the power of another number||
||Modulo||The modulo returns the remainder of the division of the number to the left by the number on its right||
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
As you can see
age is a variable that stores the computations done when subtracting
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
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.
class(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(
TRUE) 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)
 "numeric"  "numeric"  "character"  "logical"
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: