2.1 Data Types

  • As per R’s official language definitions; in every computer language variables provide a means of accessing the data stored in memory.

  • R does not provide direct access to the computer’s memory but rather provides a number of specialized data structures we will refer to as objects. These objects are referred to through symbols or variables.

2.1.1 Double

  • Doubles are numbers like 5.0, 5.5, 10.999 etc. They may or may not include decimal places. Doubles are mostly used to represent a continuous variable like serial number, weight, age etc.
x = 8.5
is.double(x)  #to check if the data type is double
[1] TRUE

2.1.2 Integer

  • Integers are natural numbers.
x = 9
typeof(x)
[1] "double"
# The following specifically assigns an integer to x

x = as.integer(9)
typeof(x)
[1] "integer"

2.1.3 Logical

  • A variable of data type logical has the value TRUE or FALSE. To perform calculation on logical objects in R the FALSE is replaced by a zero and TRUE is replaced by 1.
x = 11
y = 10
a = x > y
a
[1] TRUE
typeof(a)
[1] "logical"

2.1.4 Character

  • Characters represent the string values in R. An object of type character can have alphanumeric strings. Character objects are specified by assigning a string or collection of characters between double quotes (“ string”) . Everything in a double quote is considered a string in R.

2.1.5 Factor

-Factor is an important data type to represent categorical data. This also comes handy when dealing with Panel or Longitudinal data. Example of factors are Blood type (A , B, AB, O), Sex (Male or Female). Factor objects can be created from character object or from numeric object. -The operator c is used to create a vector of values which can be of any data type.

b.type = c("A", "AB", "B", "O")  #character object
# use factor function to convert to factor object
b.type = factor(b.type)
b.type
[1] A  AB B  O 
Levels: A AB B O
# to get individual elements (levels) in factor object
levels(b.type)
[1] "A"  "AB" "B"  "O" 

2.1.6 Date & Time

-R is capable of dealing calendar dates and times. It is an important object when dealing with time series models. The function as.Date can be used to create an object of class Date. - see help(as.Date) for more details about the format of dates.

date1 = "31-01-2012"
date1 = as.Date(date1, "%d-%m-%Y")
date1
[1] "2012-01-31"
data.class(date1)
[1] "Date"
# The date and time are internally interpreted as Double so the function typeof
# will return the type Double
typeof(date1)
[1] "double"