Contents of Introduction to R
In this Introduction to R, we are going to introduct you to the below components. We are going to cover basic understanding of What is R, variables storing, calculations, filtering, importing data, and installing packages in R.
- What is R software?
- Variables storing in R
- Calculations in R
- Filtering in R
- Importing data in R
- Installing packages in R
What is R software?
R is a programming and a statistical language used for data analysis as well as data visualisation. Writing codes in R/ R-studio is really a piece of cake. It is a free and open source language and can be easily downloaded from Google.
You can also add comments in R using Hashtag,(#). For example-
##add your comment
Using Variables in R software
Variables in R are used to store values.
|x <- 25|
x<-25 x 25
This code will store the value of x as 25 in R.
R follows BODMAS and can be easily used for calculations. It is a case sensitive programming language.
To execute the code, press ctrl + R in R & ctrl + enter in R studio
|a <- 1:5|
|a||1 2 3 4 5|
|b<- “your name”|
The variables can be used to store characters as well as numeric values of any type. Whenever you create a variable you reserve some space in memory.
Calculations in R
Calculations in R are extremely simple. Below are the example of calculations in R which can be done with no efforts.
a<-200 b <- 5 c <- a*b c ##print c 1000 d <- a/b d ##print d 40 ##print c+d c+d 1040
Similarly, we can perform other calculations in R. These are as well is very simple.
abs(-9) ## this code returns the absolute value of the function
sqrt(4) ## this code will calculate the square root of the number
log(10) ## this code will calculate the natural logarithmic (base e) value of the number
log2(2) ## this code will calculate the log value of the number with base 2
log10(2) ## this code will calculate the log value of the number with base 10
exp(1) ## this code will give exponential^(number). Here number is one so it will return exponential^1
R has a lot of inbuilt datasets which we can use for learning such as mtcars, iris , Tooth Growth and etc.
Filtering in R
Suppose we have a large dataset and we want just some values from it, that is, either the nth row or nth column, it can be done easily.
We will be using the inbuilt function iris.
iris (on executing this code, the inbuilt dataset iris will appear)
(A) It is a dataset with the 5 columns. Suppose we want to extract the first column from it.
iris[,1] (this code will give us the first column of the dataset)
(B) Suppose we want the third row of the dataset
iris[3,] (this code will give us the third row of the dataset)
(C) Suppose we want just the value of third row of second column.
(D) Suppose if we want the first “n” rows of our dataset, where “n” is any positive integer. In this example we have assumed n=10.
head(iris,10) (this code will give us the first ten rows of the dataset iris)
(E) Suppose if we want the last “n” rows of our dataset, where “n” is any positive integer. In this example we have assumed n=10.
tail(iris,10) (this code will give us the last ten rows)
Importing data to R
Suppose you to want work on some data which you have and analyse it using R. You will have to import that data to R.
Suppose you want to import the data to R which is stored in E drive of your system (hypothetical example).
##this code will tell R the directory from which it has to import file
Suppose the file is of CSV type. (you can check it by going to the properties of the file. You can also change it any other file type of your choice such as xlsx and etc)
Suppose the file is in notepad form.
##this code will enable R to read the files and the importing will be done.
Installing packages in R and calling it
A package has a lot of valuable functions which is important for data analysis and sometimes we can use few functions only after installing the packages required for them.
For example- we can plot a transition matrix of markov chain in R only after we have installed the specific package for it.
Code for installing package in R
## this code will install the required package in R. It does require an internet connection.
##this code is for calling the package so that we can use the valuable functions contained in it.
With this, our introduction to R ends. Not to mention but , it is very helpful in your CV and many other tips are covered in How to prepare for an Actuarial Job?. We hope th article Introduction to R will help you. Happy Learning.