# Progress Log 104 (R): Basic Data Types

- June 1, 2019
- nanakoohashi
- Data Analytics/Data Science, R
- No Comments

#### I’m taking an edX course entitled “Introduction to R for Data Science” and all of the concepts described below come from that course.

### Data Types

`class()`

reveals what type a variable is.

**Logical**: otherwise known as boolean values, can either be TRUE or FALSE (note: NA can also be a*logical*but we will not go into that here).- TRUE and FALSE can be abbreviated to T and F. However, it is strongly encouraged to use the full versions.

**Numeric:**The values 2 and 2.5 are called numerics in R. You can perform all sorts of operations on them such as addition, subtraction, multiplication, division, and many more. A special type of numeric is the integer.**Integer**: is a way to present natural numbers like 1 and 2. To specify that a number is an integer, you add a capital L to them.`2L`

Instead of asking for the class of the variable, you can also use the* is-dot-functions* to see whether variables are actually of a certain type.

`is.numeric()`

– used to see if a variable is numeric.`is.integer()`

– used to see if a variable is an integer.

**Character:**a string of characters.

**Other atomic types:****double:**higher precision**complex:**complex numbers**raw:**store raw bytes

### Coercion

Coercion changes the type of a variable to another one by using *dot functions*.

`as.numeric()`

– coerces the variable to a numeric. In the example below, we can see that`as.numeric(TRUE)`

coerces the logical to the numeric`1`

. Whereas`as.numeric(FALSE)`

coerces the logical to the numeric`0`

.- You can even convert characters to numerics.

`as.character()`

– coerces the original type of variable to characters.`as.integer()`

– coerces the original type of variable to an integer.- In the example below, you can see that
`"4.5"`

can be converted to an integer, but loses some information in the conversion.

- In the example below, you can see that
`as.logical()`

– coerces the original type of variable to a logical (boolean).

Coercion is not always possible. The last example below (`as.numeric("Hello"`

) returns `NA`

. R doesn’t understand how to transform `"Hello"`

to a numeric and decides to return a Not Available instead.