Please disable your adblock and script blockers to view this page

Search this blog

Monday 23 July 2018

R Data Types – Vectors, Matrices, Lists, Factors, Data Frames

 Like other programming languages, R supports many different data types. You must have seen that variables are used to store data in a program and a data type is assigned to a variable and that variable can hold only that type of data. In this post, we'll learn about R data types and R objects.

Basic data types in R programming are Numeric, Integer, Character, Logical and Complex and other than this R has some unique data types that are called R Objects.



Vectors in R

A Vector is basically a set of values of the same basic data type like numeric character etc. Vector in R is created using the c() function that represents a combination of elements.

See this example

# A Numeric Vector
numeric_vector <- c(10, 20, 30)

# A Character Vector
character_vector <- c("a", "b", "c")

# A Boolean Vector
boolean_vector <-c(TRUE,FALSE,TRUE)

The output on R Console is


Matrices in R

R supports Matrices and a Matrix is a collection of data values in 2 dimensions of the same basic data type, R creates a matrix of values using a matrix() function.

See this example

Here c(1,2,3,4,5,6,7,8,9) is a numeric vector
nrow is the number of rows in the matrix
ncol is the number of columns in the matrix


#Create a matrix using Vector
test_matrix<-matrix(c(1,2,3,4,5,6,7,8,9), nrow=3, ncol=3)
#Print matrix on console
print(test_matrix)

The output on R Console is


Arrays in R

Arrays are the same as any other programming language and in R, an array is the same as a matrix but it can have more than two dimensions. Array in R is created using the array() function and uses a vector as input and dim value to create arrays.

Here dim =c(2,2,4) means that 4 arrays will be created of 2x2.

#We have two vectors here
v1 <- c(1,2,3,7,8,9)
v2 <- c(4,5,6,10,11,12,13,14,15,16)

#Create array using vectors
test_array <- array(c(v1,v2),dim = c(2,2,4))

#print the array on the console
print(test_array)

The output on R Console is


Lists in R

A List is a set of values that can have different basic data types, In R List is created using the list() function.

#A list with different data types
#Declare a numeric vector
numeric_vector<-c(1,2,3)

#Create list 
test_list<- list("Ashish Awasthi", numeric_vector, 5.3)

#Print list
print(test_list)

The output on R Console


Factors in R

Factors are created using vectors as base and stores unique values as levels, In R Factor object is created using factor() function.


# Create a vector with duplicate values
emp_names <- c('James','Ram','James','Ashish','Ram','Ashish','James','Ram')

# Create a factor object using vector
factor_emp <- factor(emp_names)

# Print the factor object
print(factor_emp)

The output on R Console is



Data Frames in R

Data Frame is used for storing data in tables, and this tabular data can have multiple types of vectors like numeric, characters etc. Data Frame can be created using data.frame() function.

#A Character Vector
string_vector <-c("Ashish", "Awasthi","R")

#A Numeric Vector
numeric_vector <- c(10, 20, 30.5)

#A Boolean Vector
boolean_vector<- c(TRUE, FALSE, TRUE)

# Create the data frame using all 3 vectors
test_df <- 	data.frame(string_vector,numeric_vector,boolean_vector)

#Print result
print(test_df)

The output on R Console



Though this post gives an idea of using variables, In the next post, we'll learn more about using variables in R programming.

Cheers :) Happy Learning

Comments and basic arithmetic operations in R programming

 This post is about performing arithmetic operations in R programming language and putting required comments in between of code.

In R we use #(hash) sign to write a single line comment and R does not support multiline comments.

Now we'll see how to perform basic arithmetic operations in R. It is quite simple like using the calculator as no code is required to add, subtract or multiply.
See the example given below and try the code in R GUI software.



#Add Operation
10+12

#Subtract Operation
12-10

#Multiplication
10*12

#Divison
20/10

# Exponentiation
3^3

# Modulo
25%%4

And see the output in R GUI



and you can see that comments are written in code using # sign.

Print Hello World in R

The first program of every programming language is printing "Hello World" on the screen so here we'll see how to write our first program in R.
Here we take a variable sayHello and use <- to assign the value to the variable and to print this variable on the console, use the print command as shown in the below code.

#Set value in a variable 
sayHello <- "Hello World in R Programming!"
#Put this command to print
print(sayHello)

And output in R console - It is printing "Hello World in R Programming"



So this post gives a basic idea about R programming, Now in the next post, we'll learn about variables and data types used in the R language.

Saturday 21 July 2018

R tutorial - Introduction to R Programming

This R tutorial is designed for beginners who know the basics of programming and want to learn R programming.

R is an open-source programming language utilized for machine learning, factual examination, designs portrayal and detailing. R is unreservedly accessible under GNU General Public License. To learn R programming you ought to have an essential comprehension of any programming language.

The centre of R is an interpreted programming language that is the reason it doesn't require any compiler to run the code. R permits joining with the methodology written in the C, C++, .Net, Python or FORTRAN languages for effectiveness.



R was developed by Ross Ihaka and Robert Gentleman in Auckland in 1993 and supported by a group of programmers. R is the most used programming language for data analysis and statistical purposes. Like other programming languages, R supports conditional statements, looping, recursion and numerous different highlights like information representation, data visualization.

R supports number-crunching, object-oriented programming, procedural programming with functions, and has an extensive arrangement of administrators for taking care of Arrays and grids.

To start learning R programming, first of all, get R GUI Software that compiles and runs the R language.