R Programming Language - What And How With Important Functions Of R

In this article, we shall discuss about basics of R programming language and we shall also discuss some important functions of R programming language.

Introduction

R is an important programming language which is used by statisticians. It is also used in machine learning, data science, and research.

This is a free and open source software language. There are several packages available in R which are used for different types of functionality to be performed.

In this article, we shall discuss how to work and implement R programming language. After that, we shall discuss some of the important functions of R language.

Let's start now with the basics of R.

R Working Environment

We can work with R in RStudio. It can be downloaded from online and it is free. Once you install RStudio and open it will look like the below window.

RStudio has four separate windows integrated within it. They are,
  • Code Editor
  • R Console
  • Workspace and History
  • Plot and files
R

Let's discuss about these windows briefly.
  • Code Editor
    We can create R script file and can do our R programming stuff related to our functionality in Code Editor. We can save and open R script file in code editor for future reference. We can use file menu for opening and creating an R script file. Everything  created with .R extension.

  • R Console
    Everything that we create and type in Code Editor is also reflected in R console. We can execute code from R console. The basic difference between Code Editor and R console is that we can save, reopen and edit Code Editor file but it can not be done with R console. The files and codes written in R console remain in computer RAM so once we close the system it lost the data.

  • Workspace and History
    This window displays the history of stuff we did earlier. It displays the variable with the data currently available with those variables.

  • Plot and files
    This window displays different types of files available for working. It also displays the plots, chart, and area used or plotted during R programming.
Getting help in R

Getting help and description of any function in R is very impressive. Once you are stuck on any point just use one of the below methods,
  • ?(<function name>)
  • help(<function name>)
For example

Once you type help (type) it will display the detailed description of type function in the help window as below,

R 

Setting and getting Working Directory in R

The working directory is the location of our computer where our script related to R, codes, files and all other stuff is stored. It helps us generally to work fast when we work with read and write operations as these operations require file path, for example, reading Excel data in R. If the file which we want to access is in the same directory, then no full path is required it only require the file name to be read.

To set the working location of use setwd() function. For e.g.,

R 

To get the location of the working directory, we use getwd() function. For e.g.,

R

Comment in R

It is an important part of the code which is used for documentation. Although it is not executable yet it helps others to understand the code we have done. So, every language supports this functionality. For commenting R code, we use # sign before it.

R 

Datatypes and Variable in R

R language supports a wide range of datatypes like numeric, integers, logical, character, list, matrices, vector, etc. We shall discuss it later in another article.

Working with Variables in R

In R variables datatype is assigned automatically runtime as per the value assigned to it. To assign a value in any variable we use sign <-. However, we can use = operator also to assign a varible in R.

Below are some of the variables used with basic operations performed.

R 

Checking Datatype of Variables in R

To know the datatype of the variable just we use the typeof() function in R. Below images are the example of typeof() function.

R 

Creating our own function in R

We use keyword function for creating a function in R. The syntax is very simple just give the name of the function with the assignment operator. The below screen illustrates how to create a function in R. Here, Addition, Subtraction, Multiplication, and Division are the name of functions. All these functions have two parameters named a and b.

R 

Calling a function in R

To call a function just type the name of the function with their parameter values. For example, calling the above functions is as simple as it has been shown in below image.

R 

To make a parameter optional you can assign it while creating a function. For example, it has been shown in the below image. Here, we have passed only the value of a and b is assigned automatically as 10.

R

Concatenating two strings or characters in R

We use paste function for concatenating two string or characters. You can use it as in below image.

R 

Summary

In this article, we have learned the basics of R programming language. We have also learned some important functions like setwd(), getwd(), help(), paste() and print(). Apart from this, we have also learned how to create and call our own functions in R.

Hope you have learned and enjoyed reading this article. You’re welcome to like, comment, share and any type of suggestion for my appreciation.