# Introduction to R (part 2)

# Basic data types in R

R works with numerous data types. Some of the most basic types to get started are:

- Decimal values like 4.5 are called
**numerics**. - Natural numbers like 4 are called
**integers**. Integers are also numerics. - Boolean values (TRUE or FALSE) are called
**logical**. - Text (or string) values are called
**characters**.

## Constants in R

Constants, as the name suggests, are entities whose value cannot be altered. Basic types of constant are numeric constants and character constants.

**Numeric Constants**

All numbers fall under this category. They can be of type `integer`

, `double`

or `complex`

.

# Change my_numeric to be 42

my_numeric <- 42# Check class of my_numeric

class(my_numeric)

[1] 'numeric'typeof(5)

[1] "double"

typeof(5L)

[1] "integer"

typeof(5i)

[1] "complex"

Numeric constants preceded by `0x`

or `0X`

are interpreted as hexadecimal numbers.

`0xff`

[1] 255

0XF + 1

[1] 16

**Character Constants**

Character constants can be represented using either single quotes (‘) or double quotes (“) as delimiters.

> 'example'

[1] "example"

> typeof("5")

[1] "character"# Change my_character to be “universe”

my_character <- “universe”# Check class of my_character

class(my_character)

'character'# Change my_logical to be FALSE

my_logical <- FALSE

**Built-in Constants**

Some of the built-in constants defined in R along with their values is shown below.

`> LETTERS`

[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"

[20] "T" "U" "V" "W" "X" "Y" "Z"

> letters

[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"

[20] "t" "u" "v" "w" "x" "y" "z"

> pi

[1] 3.141593

> month.name

[1] "January" "February" "March" "April" "May" "June"

[7] "July" "August" "September" "October" "November" "December"

> month.abb

[1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"

## Logical

- True
- False

my_logical <- FALSE# Check class of my_logical

class(my_logical)[1] ‘logical

# R Arithmetic Operators

These operators are used to carry out mathematical operations like addition and multiplication. Here is a list of arithmetic operators available in R.

`x <- 5`

y <- 16

> x+y

[1] 21

x-y

[1] -11

x*y

[1] 80

y/x

[1] 3.2

y%/%x

[1] 3

y%%x

[1] 1

y^x

[1] 1048576

# R Relational Operators

Relational operators are used to compare between values. Here is a list of relational operators available in R.

`x <- 5`

y <- 16

x<y

[1] TRUE

x>y

[1] FALSE

x<=5

[1] TRUE

y>=20

[1] FALSE

y == 16

[1] TRUE

x != 5

[1] FALSE

# Mathematical functions

In R, of course, you want to use more than just basic operators. R comes with a whole set of mathematical functions you’d find on a technical calculator as well

**Calculating logarithms and exponentials In R**

You can take the logarithm of the numbers from 1 to 3 like this:

log(1:3) [1] 0.0000000 0.6931472 1.0986123

Whenever you use one of these functions, R calculates the natural logarithm if you don’t specify any base. You calculate the logarithm of these numbers with base 6 like this:

log(1:3, base = 6)

[1] 0.0000000 0.3868528 0.6131472

You carry out the inverse operation of `log()`

by using `exp()`

. This last function raises e to the power mentioned between parentheses, like this:

**Scientific notation**

If you raise numbers to a power, the result will quickly become a very large number. For example, if you raise 1000 to the power of 2, you get a million. If you try that in R, you see the following:

1000^2 [1] 1e+06

R automatically decides whether to print a number in scientific notation. Its decision to use scientific notation doesn’t change the number itself, nor the accuracy of the calculation; it just saves some space.

**Rounding numbers**

Although R can calculate accurately to up to 16 digits, you don’t always want to use that many digits. In this case, you can use a couple functions in R to round numbers. To round a number to two digits after the decimal point, for example, use the `round()`

function as follows:

round(123.456, digits = 2) [1] 123.46

You also can use the `round()`

function to round numbers to multiples of 10, 100, and so on. For that, you just add a negative number as the digits argument:

round(-123.456, digits = -2) [1] -100

If you want to specify the number of significant digits to be retained, regardless of the size of the number, you use the `signif()`

function instead:

signif(-123.456, digits = 4) [1] -123.5

Both `round()`

and`signif()`

round numbers to the nearest possibility. So, if the first digit that’s dropped is smaller than 5, the number is rounded down. If it’s bigger than 5, the number is rounded up. If the first digit that is dropped is exactly 5, R uses a rule that’s common in programming languages: Always round to the nearest even number. round(1.5) and round(2.5) both return 2, for example, and round(‐4.5) returns ‐4.

Functions always round in the same direction:

`floor(x)`

rounds to the nearest integer that’s smaller than x. So floor(123.45) becomes 123 and floor(‐123.45) becomes –124.`ceiling(x)`

rounds to the nearest integer that’s larger than x. This means ceiling(123.45) becomes 124 and ceiling(‐123.45) becomes –123.`trunc(x)`

rounds to the nearest integer in the direction of 0. So trunc(123.65) becomes 123 and trunc(‐123.65) becomes –123.

**Trigonometric functions**

All trigonometric functions are available in R: the sine, cosine, and tangent functions and their inverse functions.

cos(120) [1] 0.814181

This code doesn’t give you the correct result, however, because **R always works with angles in radians**, not in degrees.