4 – R Data Types and Data Structures 

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. 

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 

30


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” 


Logical 

A variable of data type logical has the value TRUE or FALSE. T 

x = 11 

y = 10 

a = x > y 

# [1] TRUE

typeof(a) 

# [1] “logical” 

To perform calculation on logical objects in R the FALSE is replaced by a zero and TRUE is replaced by 1. 


4.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. 

x = “This is a string” 

print(x) 

# [1] “This is a string” 

x = “a” 

typeof(x) 

# [1] “character”


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” 


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.1 Tip: Use args(function name)to see the various arguments in a function. 1See help(as.Date)for more details about the formats 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” 

• R has two inbuilt classes POSIXct and POSIXlt to deal with date and time which can be used to repres ent calendar dates and times. 

• A character date or time can be converted to these two classes by calling the function as.POSIXct to create a POSIXct object. This function accepts date, time or date with time as character input and uses a format argument to specify a non default format. A time zone can also be specified when dealing with a specific time zone 2 

2See help(as.POSIXct)or help(as.POSIXlt) for further details. strptime is a very useful function to convert one format of date and


Missing Data in R 

• Datasets available for research often has missing data. In R missing data is represented by NA (Not 

Available), it can be any missing data type. Another symbol to represent missing number is NaN (Not a 

Number). 

• The following example shows how to detect missing values in data vector. NULL in R represents a null object with length zero or for an undefined object 

m.data = c(“100”, “200”, “missing”) 

# convert m.data to double will create one missing value as ‘missing’ # is not a double 

m.data = as.double(m.data) 

# the warning message tells that an NA was insterted for a value # which couldnt be converted to type double 

is.na(m.data) #check for the missing value 

# [1] FALSE FALSE TRUE 

time in character to another. See help(strptime)for the different date/time formats.

We often come across +- Infinite values in the models (for instance division by zero). Inf, Inf represent negative and positive infinite values in R. 


Data Structures in R 

Every data analysis requires the data to be structured in a well defined way. These coherent ways to put together data forms some basic data structures in R. Every data set intended for analysis has to be imported in R environment as a data structure. R has the following basic data structures: 

• Vector 

• Matrix 

• Array 

• Data Frame 

• Lists


Vector

  • A vector in R is a collection (or group) of values that all share the same data type.
  • Vectors can contain numbers, characters, or logical values (TRUE/FALSE). They are often used to represent a single variable in a dataset.
  • You can create a vector using the c() function, which stands for combine. This function can also be used to join multiple vectors of the same data type together.

Example:

# Creating a numeric vector
vec1 <- c(1, 2, 3, 4, 5)

vec1
# [1] 1 2 3 4 5

In this example, vec1 is a numeric vector containing five elements: 1, 2, 3, 4, and 5.


Matrices

  • A matrix is a two-dimensional structure that organizes data in rows and columns, forming a rectangular layout.
  • Just like vectors, all elements in a matrix must be of the same data type — for example, all numeric or all character values.
  • You can create a matrix in R using the matrix() function. This function allows you to specify the data, number of rows, and number of columns.

Matrix Manipulations

  • In R, matrices can be created by combining two or more vectors — either by columns or by rows.
    • For example, the functions cbind() (column bind) and rbind() (row bind) are commonly used for this purpose.
  • You can perform mathematical operations on matrices just like you do with vectors — functions such as addition, subtraction, multiplication, and division all work on matrices.
  • A matrix can also be multiplied by a vector as long as the vector’s length is compatible with the matrix’s dimensions (i.e., the vector length must be a multiple of the matrix size).
    • Try experimenting with different combinations of matrix and vector arithmetic to understand how R handles them and what errors occur when dimensions don’t match.
  • R also supports true matrix algebra operations.
    • For example, the operator %*% is used for matrix multiplication, but remember — the matrices must have compatible dimensions for the operation to work correctly.

Arrays

  • An array in R is a more general form of a vector or matrix.
  • While a vector is one-dimensional and a matrix is two-dimensional, an array can have two or more dimensions.
  • In simple terms, an array is a multi-dimensional collection of data elements, where all values must be of the same data type (e.g., all numeric or all character).

Data Frames

  • A data frame is one of the most important and convenient data structures in R for handling tabular data — that is, data organized in rows and columns.
  • In quantitative research, data is usually collected in the form of tables, where:
    • Each row represents an observation or record (e.g., one person, company, or transaction).
    • Each column represents a variable or attribute (e.g., age, income, or stock price).
  • A data frame in R is designed specifically for this type of data. Unlike matrices or arrays, data frames can hold different data types in different columns — for example:
    • One column may contain numbers (numeric data),
    • Another may contain text (character data), and
    • Another may contain logical values (TRUE/FALSE).
  • Because of this flexibility, data frames are the standard input format for most statistical, analytical, and visualization functions in R. They provide a natural and powerful way to manage, analyze, and model data in real-world applications.

In short:

A data frame is like a spreadsheet inside R — flexible, organized, and perfect for real-world data analysis.

Pages: 1 2 3 4