Basic Introduction To R Programming Language

VICTOR OMONDI
The Startup
Published in
4 min readMay 16, 2020
Poster: Basic Introduction To R

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.

Poster: Goal

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 https://en.wikipedia.org/wiki/R_(programming_language)

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.

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

print prints its argument and returns it invisibly

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

Using Print Function to 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 names
  • Variable names are Case Sensitive, therefore, my_name is different from my_Name.

Below are examples of correct variable names and assignment in R:

Various Types of naming a variable

Their output are as follows:

[1] 17
[1] "Victor"
[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:

R Arithmetic Operators and their functions

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.

Using R Arithmetic Operators to perform calculations
[1] 19

As you can see age is a variable that stores the computations done when subtracting year_of_birth from current_year.

Comments in R

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.

Comments in R
[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. Use 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( FALSE or TRUE) are called logical. R is case sensitive, logicals must be in UPPERCASE
  • String values/text values are called characters
[1] "numeric"
[1] "numeric"
[1] "character"
[1] "logical"

More Resources

  • R Introduction | R Tutorial: link
  • Introduction to R Online Course | DataCamp: link

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 the comments.

References

Poster: Thank you

--

--

VICTOR OMONDI
The Startup

Data Scientist |@AgoraIO, @Ingressive, @MoringaSchool & CodeLn Campus Ambassador | @DSC_MMU Lead | @CITClubMMU Ass.Chairman| Mentor @Technovation & @AndelaALC