1. Home
  2. Training Library
  3. Big Data
  4. Courses
  5. 4. Intermediate Data Structures in R

Implicit coersion in R

Developed with


Fundamentals of R
Objects in R
Integers in R

The course is part of this learning path

Fundamentals of R
Start course


Course Description 

This module looks at more complex data structures, building on what was covered in the beginner data structures module.   

Learning Objectives 

The objectives of this module are to provide you with an understanding of: 

  • Different data types  
  • Integers  
  • How to coerce elements, and force coercion  
  • How to construct a matrix  
  • How to construct an array 
  • How to construct a list 

Intended Audience 

Aimed at all who wish to learn the R programming language. 


No prior knowledge of R is assumed 

Delegates should already be familiar with basic programming concepts such as variables, scope and functions 

Experience of another scripting language such as Python or Perl would be an advantage 

Understanding mathematical concepts will be beneficial 


We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think. 


- [Instructor] In order to explain Implicit Coercion in R, I'd like to use a vector. Vectors are one-dimensional and each element of a vector must be of the same or common type. So below, I'm going to explain some examples where R will implicitly convert elements to the same or common type. First let's think about moving from a logical to an integer. So if I was to create a vector, of both logicals true and of integers 2L, we could see after running this command that we only have a return of one and two on the screen. All elements are coerced to the integer type. And I can prove that by running the class function to see the class of this as being integer. We can expand this now to consider what happens if we include, logicals and numerics and integers in our vector. So if I was to create a class of-- Let's start firstly with a true and a numeric. In this case here, now we might ask, "What is the class of this?" In this case, because I have the character 2 as opposed to the character 2L. I had an integer before now I have a numeric true is coerced into a numeric of one. I can repeat this now for another example where I have a Boolean, an integer and a numeric and we can find out that all elements have been coerced to numeric. I can repeat the same again. But this time using the full length of the word true and again R will coerce all elements as part of this vector into numerics. I can, for example create a vector of trues and falses, a Boolean vector and I can combine these, utilizing a variable assigned to X being true, false, true. And I can return back, this to the screen to show what I have created, which is 1.2 followed by a series of trues and falses which have been coerced to numerics. And I can ask for the class of this on the screen, plus numeric. The final rule, coercion of logicals all the way through to characters and I can show on the screen a simple example where we use all three Rs, strings or characters and thus I'd like to just explain that we have the character vector up here in Y and now utilizing-- So if I call to the screen X that we had earlier, which was a Boolean and I now pull out a combination of my numerics my Y which is my character vector and X which is my Boolean vector and I see at the end of this, that everything will get coerced to characters. So if I ask for the class of Z I can see that we have a character vector. A final point to note now is that nonsensical coercions will fail. Say for example, I asked for a series of a vector of strings or characters and I asked for them to be coerced into numerics, this would return NAs. If I asked for some logical from this, this again would return NAs. So nonsensical coercions will fail.

About the Author
Learning paths1

Kunal has worked with data for most of his career, ranging from diffusion markov chain processes to migrating reporting platforms.  

Kunal has helped clients with early stage engagement and formed multi week training programme curriculum. 

Kunal has a passion for statistics and data; he has delivered training relating to Hypothesis Testing, Exploring Data, Machine Learning Algorithms, and the Theory of Visualisation. 

Data Scientist at a credit management company; applied statistical analysis to distressed portfolios. 

Business Data Analyst at an investment bank; project to overhaul the legacy reporting and analytics platform. 

Statistician within the Government Statistical Service; quantitative analysis and publishing statistical findings of emerging levels of council tax data. 

Structured Credit Product Control at an investment bank; developing, maintaining, and deploying a PnL platform for the CVA Hedging trading desk.