0

R for Data Science – Part 3 – Data Types and Objects

Share this article!

This is the third part of the series “R for Data Science”. The series is as follows:

Like many other programming languages, the variables in R also have their own data type which can be numeric, integer, logical, complex or character. To assign the data type to a variable, we use R-objects as the data type of R-objects is the data type of the variable. The following are the R-objects:

  • Vectors
  • Matrices
  • Factors
  • Data Frames
  • Lists

 

Vectors

Vector is a one-dimensional array which is use to store same type of data types like numeric, character and Boolean (logical) values in it. To create a vector in R we use combine function i.e. c() and elements are separated by comma in it.

 

#numeric vector

> numeric = c (45,83, 12,28,71,94)

#character vector

>character = c("Siddhartha ", "Aman ", "Piyush ")

#Boolean vector

>boolean = c(True, False, False, True, False)

 

We can also assign name to a vector with the names() function.

 

#create vector

> vector= c("Sunday ", "Mothers_Day ")

#naming the vector

> names(vector) = c("Week_Day ", "Event ")
> vector

 

The output of above code is:

 

Week_Day         Event

"Sunday"      "Mothers_Day"

 

Matrices

Matrices consist of same data type elements having rows and columns and a matrix ids defined using matrix() function.

 

#create matrix

> mx = matrix(c(45,58,96,100,52,15), nrow = 3, ncol=2,byrow= TRUE)

> mx

 

The printed matrix is:

 

      [,1] [,2]

[1,]   45   58

[2,]   96  100

[3,]   52   15

 

where a byrow element signifies the existence of rows in the matrix and nrow and ncol represents the number of columns in it. A vector can also be converted into a matrix with the matrix() function.

 

#create vector

> marks= c(85,74,56,92)

#create matrix

> marks_matrix= matrix(marks, nrow=2)

> marks_matrix

 

The output is:

 

      [,1] [,2]

[1,]   85   56

[2,]   74   92

 

Factors

 

A factor consists of the vectors and the distinct elements in the vector as labels. The labels in factor are character type regardless of the data types of the elements in vector. We use factor() function to create the factor object.

 

#create vector

> flowers= c("Rose ","Lily ","Rose ","Lotus ","Lily ")

#create factor

> flowers_factor= factor(flowers)

> flowers_factor

 

The output of above code is:

 

[1] Rose Lily Rose Lotus Lily

Levels: Lily Lotus Rose

nlevels() function is used to calculate the total number of levels in factors.

 

#calculate levels of factor

> nlevels(flowers_factor)

[1] 3

 

Data Frames

Data frames can store different data types in a tabular form. To create a data frame we use data.frame() function in which we use vectors as elements provided that the number of elements  in all vectors is equal.

 

> a= c("Gallery ", "Music ", "Games ")

> n= c(56789,7845612, 56978)

> l= c(TRUE,TRUE,FALSE)

> dframe= data.frame(a, n, l)

#create data frame

> dframe

 

The output that we get after executing the code is:

 

        a       n     l

1  Gallery   56789  TRUE

2   Music  7845612  TRUE

3   Games   56978  FALSE

 

Lists

A list can store other R-objects including vectors, matrices, data frames, functions and other lists within itself. It allows us to access different objects under same name. A list can be created using list() function.

 

>vector= c(78,85,56,94)

#create list

>my_list= list(vector,  sin(47), 85.21)

> my_list

 

The output is:

 

[[1]]

[1] 78 85 56 94

[[2]]

[1] 0.1235731

[[3]]

[1] 85.21

 

This is all for the lesson and now you can try the data types and objects in R.

Please do comment if you have any query or doubt regarding the topic. Till then Happy Learning.

Share this article!

Shrishti Jain

Shrishti Jain

I am a third year B.Tech student and passionate learner of data science and R. Looking forward for the opportunities to explore more in these fields along with Java.

LinkedIn: www.linkedin.com/in/shrishti-jain-760899135
Shrishti Jain

Latest posts by Shrishti Jain (see all)

Shrishti Jain

I am a third year B.Tech student and passionate learner of data science and R. Looking forward for the opportunities to explore more in these fields along with Java. LinkedIn: www.linkedin.com/in/shrishti-jain-760899135

Leave a Reply

Your email address will not be published. Required fields are marked *