In R decimal values are called `numeric`

. By assigning a decimal value to a variable `num`

, `num`

will be of numeric type.

```
num <- 33.33
num
```

`## [1] 33.33`

`class(num)`

`## [1] "numeric"`

Even if we assign an integer to a variable `iteg`

, it is still being saved as a numeric value.

```
iteg <- 33
iteg
```

`## [1] 33`

`class(iteg)`

`## [1] "numeric"`

In order to create an integer variable in R, we state this explicitly by adding an *L*, or by invoking the `as.integer()`

function.

```
iteg <- 50L
iteg
```

`## [1] 50`

`class(iteg)`

`## [1] "integer"`

```
iteg2 <- as.integer(25)
iteg2
```

`## [1] 25`

`class(iteg2)`

`## [1] "integer"`

```
iteg2 <- as.integer(5.55)
iteg2
```

`## [1] 5`

`class(iteg2)`

`## [1] "integer"`

Logical values relate to comparisons between variables.

```
x <- 100
y <- 200 # sample values
z <- x > y
z
```

`## [1] FALSE`

`class(z)`

`## [1] "logical"`

Standard logical operations are `>`

, `<`

, `>=`

, `<=`

, `&`

(and), `|`

(or), and `!`

(negation).

```
j <- TRUE
k <- FALSE
j & k
```

`## [1] FALSE`

`j | k`

`## [1] TRUE`

`!j`

`## [1] FALSE`

A character object is used to represent string values in R. It is created by assigning a *string*, a sequence of characters, to a variable or by converting objects into character values with the `as.character()`

function:

```
chr <- "soga"
class(chr)
```

`## [1] "character"`

```
p <- as.character(3.14)
class(p)
```

`## [1] "character"`

There are many functions available to work on `character`

objects.

Two character values can be concatenated with the `paste()`

or the `paste0()`

function.

```
first <- "Francis Ford"
last <- "Coppola"
paste(first, last)
```

`## [1] "Francis Ford Coppola"`

`paste0(first, last)`

`## [1] "Francis FordCoppola"`

We may extract a substring by applying the `substr()`

function.

```
my.string = "I prefer statistics over beer and coffee"
substr(my.string, start = 3, stop = 22)
```

`## [1] "prefer statistics ov"`

Or play around with upper and lower case transformations.

`toupper(my.string)`

`## [1] "I PREFER STATISTICS OVER BEER AND COFFEE"`

`tolower(my.string)`

`## [1] "i prefer statistics over beer and coffee"`

Many more functions for string manipulation are available (see for example the `stringr`

package).

Factors are used to represent categorical data. They are a very useful class for statistical analysis and for plotting. Note that factors can be **ordered or unordered**.

Factors can be created using the `factor()`

command.

`weather <- factor(c("rainy", "sunny", "sunny", "rainy"))`

Factors are stored as integers, and have labels, known as *levels*, associated with these unique integers. By default, R always sorts levels in alphabetical order.

`weather`

```
## [1] rainy sunny sunny rainy
## Levels: rainy sunny
```

R will assign 1 to the level “rainy” and 2 to the level “sunny”. We can check this by using the `str()`

function:

`str(weather)`

`## Factor w/ 2 levels "rainy","sunny": 1 2 2 1`

If the order matters and is meaningful (e.g., “low”, “medium”, “high”) we may specify the order of the levels by the `levels`

argument, and by the `ordered = TRUE`

argument:

```
flood.level <- factor(c("low", "high",
"medium", "high",
"low", "medium"),
levels = c("low", "medium", "high"),
ordered = TRUE)
min(flood.level)
```

```
## [1] low
## Levels: low < medium < high
```