#### Numeric

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
##  33.33
class(num)
##  "numeric"

Even if we assign an integer to a variable iteg, it is still being saved as a numeric value.

iteg <- 33
iteg
##  33
class(iteg)
##  "numeric"

#### Integer

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
##  50
class(iteg)
##  "integer"
iteg2 <- as.integer(25)
iteg2
##  25
class(iteg2)
##  "integer"
iteg2 <- as.integer(5.55)
iteg2
##  5
class(iteg2)
##  "integer"

#### Logical

Logical values relate to comparisons between variables.

x <- 100
y <- 200   # sample values

z <- x > y
z 
##  FALSE
class(z)
##  "logical"

Standard logical operations are >, <, >=, <=, & (and), | (or), and ! (negation).

j <- TRUE
k <- FALSE

j & k
##  FALSE
j | k
##  TRUE
!j
##  FALSE

#### Character

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)
##  "character"
p <- as.character(3.14)
class(p)
##  "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)
##  "Francis Ford Coppola"
paste0(first, last)
##  "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)
##  "prefer statistics ov"

Or play around with upper and lower case transformations.

toupper(my.string)
##  "I PREFER STATISTICS OVER BEER AND COFFEE"
tolower(my.string)
##  "i prefer statistics over beer and coffee"

Many more functions for string manipulation are available (see for example the stringr package).

#### Factor

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
##  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)
##  low
## Levels: low < medium < high