<img alt="" src="https://secure.leadforensics.com/150446.png " style="display:none;">
Go to top icon

Beginner's guide to R

Abhay Wagh Nov 07, 2017

language R R language

Beginner's guide to R.jpg

R is a Statistical Programming Language used for statistical computing, data analysis and graphics. It is an open source and widely used language by data scientists for data analysis. There are various software which supports integration with R, for example you can integrate R with SQL server.  

This blog will give you the beginner's guide to Language R.  

Basic data types in R 

There are four basic data types in R.  

  • Numeric- values like 1.2 
  • Integer - values like 1 
  • Logical - TRUE or FALSE 
  • Character - Text or String values 

Arithmetic operations  

Following arithmetic operations can be applied on numeric data: 

  • Addition (+) - adding numbers ex. 1.2+ 1 is 2.2 
  • Subtraction (-) - subtracting numbers ex. 1.2-1 is 0.2 
  • Multiplication (*) - multiplying numbers 1.2 * 2 is 2.4 
  • Division (/) - division of numbers 4/2 is 2 
  • Exponentiation (^) - power of number 5^2 is 25  
  • Modulo (%%) - returns the remainder of the division of two numbers ex. 5%%2 is 3 

Variable assignment 

Similar to other languages, variable allows you to store values. In R, values are assigned to variable by using “<-” sign. 

VarString <- “Language R”- this will create character variable with name “VarString” and assign value “Language R” to it. 

VarInteger <- 1- this will create integer variable with name “VarInteger” and assign integer value 1. 

In case of numeric or integer type of variables arithmetic operations can be performed. 

VarA <- 5
VarB <- 6
VarC<- 5.6
VarD <- varA + varB- value (5+6) i.e. 11 will be assign to variable “varC”
VarE <- varA + varC- will add variable of integer data type and variable of numeric data type and assign value 10.6 to variable "varE"
VarString <- “Language R”
VarF <- varA + VarString - this will give you an error as you are trying two different data types Arithmetic operations will work on numeric and integer data types
VarLogical <- TRUE

How to get data type from variable 

Class() function can be used to identify the data type. You can avoid error by verifying data type. For example, before adding variables you can check if both variables are of numeric data type. 

Class(varString)- will give you result as “character”
Class(varA)- will give you result as “numeric”
Class(varLogical)- will give you result as “logical”

Logical operators  

Following are the logical operators in R 

  • <- less than ex. 4 < 8  
  • <=- less than or equal to ex. 4<= 8  
  • >- greater than ex. 8 > 4 
  • >=- greater than or equal to ex. 8 >= 4 
  • ==- exactly equal to ex. varX == 4 
  • !=- not equal to ex. varX != 8 
  • !x- Not x   
  • x | y- x OR y 
  • x & y- x AND y 
  • isTRUE(x)- test if X is TRUE 

Vectors 

Vectors are one dimensional array which can hold any type of data. To create vector, use function c(). Elements of vector are placed inside parenthesis separated by comma.  

nVector <- c( 1,2,3,4,5)- one dimensional array with numeric elements 

cVector <- c(“a”,”b”,”c”)- one dimensional array with character elements 

Matrix 

Matrix is two dimensional array. Each element in matrix is of same data type arranged in rows and columns. To create Matrix use function matrix() 

Let’s take an example of student’s marks in Maths, Science and English subject 

MathsScienceEnglish 

Student A708090
Student B728292
Student C738393

Create three vector 

vStudentA <- c(70,80,90)
vStudentB <- c(72,82,92)
vStudentC <- c(73,83,93)

Create vector from above vector  

vClass8 <- c(vStudentA, vStudentC, vStudentC) 

Create matrix 

Matrix(vClass8 , byrow =TRUE , nrow=3 , ncol=3) 

Where nrow - no of rows in matrix
Where ncol - no of columns in matrix
Where byrow – fill matrix by row

Data Frames 

Data frames are used to store data in tabular formats or a two dimensional objects. Each column can store same type of data and columns can have different data types. Data frames can be created using function data.frames(). 

For ex.
CityLongitudelatitude

Orai25.98983679.450035
Surajpur23.22304782.870560
Ambernath19.18635473.191948

Create three vector 

vCity <- c(“Orai”,” Surajpur”,” Ambernath”)
vLongitude <- c(25.989836, 23.223047, 19.186354)
vlatitude <- c(79.450035, 82.870560, 73.191948)

Create matrix 

data.frame(vCity, vLongitude, vlatitude  ) 

In Summary, these are the basic elements of Language R and will be used in R programs for data analysis and storing data. In my next blog, I will explain Vectors, Matrix and data frames in detailIf you like this post share your valuable feedback. 

 

Similar Blog

In my last blog Beginner's guide to R, I have written about the data types, variable, arithmetic and logical operations and ...

By Abhay Wagh Nov 23, 2017

e-Zest is a leading digital innovation partner for enterprises and technology companies that utilizes emerging technologies for creating engaging customers experiences. Being a customer-focused and technology-driven company, it always helps clients in crafting holistic business value for their software development efforts. It offers software development and consulting services for cloud computing, enterprise mobility, big data and analytics, user experience and digital commerce.