- Home
- Training Library
- Programming
- Courses
- 4. Intermediate Data Structures in R

# Explicit coersion in R

## Contents

###### Fundamentals of R

## The course is part of this learning path

**Difficulty**Intermediate

**Duration**40m

**Students**22

**Ratings**

### Description

**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.

**Pre-requisites**

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

**Feedback**

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

### Transcript

- [Instructor] When we talk about an explicit coercion in R, we're talking about forcing a coercion, or a conversion, to a specific type. Say, for example, I had a TRUE or a Boolean or a logical item of data, I wanted to understand the class of this, I can see that it is logical at the moment. I can coerce this or convert this into a numeric. I can also convert TRUE, where T is the abbreviation for TRUE into a character, I will automatically pick up all four letters T, R, U, E in a piece of character data. And a redundant function in this case, but just to explain what this does is it converts whatever you input in here into a logical TRUE or FALSE. We can show another explicit coercion from a string, now the class of this would be character. And in the same way as above, I can ask for the character of this. And I can convert it from a character to a character. So this is almost again, an unnecessary statement. Can I convert a letter or a character into a Boolean of TRUE and FALSE? The answer is Boolean or logicals in R include NA. So we won't see a warning on the screen but implicitly or during this explicit coercion, implicitly, R understands that this letter here can't represent a TRUE or a FALSE in such that it returns no warning and it tells you that this is an NA. This may be something you should be aware of in case you decide to ever use the as logical as something to be weary of. Can I convert the A as a character into a number? The answer is that doesn't really make sense. So R returns a warning to say NAs have been introduced by coercion. And if I'd like to understand 2.3 I can see that it is A, numeric and I can use the simple as numeric conversion to convert 2.3 into a numeric which is a again, almost nonsensical redundant in this case, I can convert 2.3 into a character which converts 2.3 into a piece of string data. Can I convert 2.3 into a logical? Now previously we said a character cannot be converted into a logical of TRUE or FALSE and so it is returned as an NA. However, if I convert 2.3 into a logical, there is no warning, and it returns TRUE. Numerics are converted to logicals without any issue. In fact, the only numeric that is converted into FALSE is zero. Any other number coerces to TRUE whether it's plus 99, minus 99, or even if I took 0.99 negative, it handles all numbers the same except zero returns to FALSE.

# About the Author

**Students**251

**Labs**1

**Courses**11

**Learning paths**1

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.