Chapter 3 Creating Objects

Within R objects are stored within memory, allowing the object to be quickly accessed for analysis, modelling, and visualization. A common use of objects is to store data, however objects can be used to store many things, such as results from an equation, output from an analysis, functions, and even plots. In order to commit something to an object the <- assignment operator must be used. The following basic math equation will create a data object called x x <- 1 + 1, where the same basic math equation without the assignment operator will not create a data object 1 + 1

This section will focus on creating data objects with later sections showing how the assignment operator can be used to make different objects.

3.1 Variables

A variable is the storage of a single object. Variables can be created from providing specific values, or as a result of combining multiple variables together through adding, subtracting, multiplying, dividing, or a function.

3.1.1 Create a character variable

Description
Create a variable object in memory for a specific character value
Ingredients
Package Data
NA NA


Sample Instructions

object_name <- "text"


Actual Instructions

character_variable <- "Halifax"

3.1.2 Create a numeric variable

Description
Create a variable object in memory for a specific number value
Ingredients
Package Data
NA NA


Sample Instructions

object_name <- number


Actual Instructions

number_variable <- 10

3.1.3 Create a variable from an equation

Description
Create a variable object in memory for a specific mathematical equation
Ingredients
Package Data
NA NA


Sample Instructions

object_name <- number + number


Actual Instructions

total_variable <- 10 + 10

3.1.4 Create a variable from combining variables

Description
Create a variable object in memory by combining two variable objects together
Ingredients
Package Data
NA NA


Preparation

first_number_variable <- 5
second_number_variable <- 10


Sample Instructions

object_name <- variable1 + variable2


Actual Instructions

combined_variable <- first_number_variable + second_number_variable

3.1.5 Create a variable from a function

Description
Create a variable object in memory from the output from the system date function
Ingredients
Package Data
NA NA


Sample Instructions

object_name <- function()


Actual Instructions

current_date_variable <- Sys.Date()

3.2 Vectors

A vector is a simple data structure that stores a sequence of values. A requirement of a vector object is that all objects be of the same data type (i.e. character, integer, numeric, or logical). While basic vectors are a very useful data structure that are helpful in various data analytics situations.

3.2.1 Create a character vector

Description
Create a vector object in memory that has multiple character values
Ingredients
Package Data
NA NA


Sample Instructions

object_name <- c("text", "text", "text")


Actual Instructions

character_vector <- c("one", "two", "three")

3.2.2 Create a numeric vector

Description
Create a vector object in memory that has multiple numeric values
Ingredients
Package Data
NA NA


Sample Instructions

object_name <- c(number, number, number)


Actual Instructions

number_vector <- c(1, 2, 3)

3.3 Dataframe

A vector is a simple data structure that stores a sequence of values. A requirement of a vector object is that all objects be of the same data type (i.e. character, integer, numeric, or logical). While basic vectors are a very useful data structure that are helpful in various data analytics situations.

3.3.1 Create dataframe with multiple character columns

Description
Create a dataframe object in memory that has multiple character columns
Ingredients
Package Data
NA NA


Sample Instructions

dataframe_name <- data.frame(first_column_name = c("text", "text", "text", "text"), second_column_name = c("text", "text", "text", "text"))


Actual Instructions

atlantic_provinces <- data.frame(province = c("NL", "NS", "PE", "NB"), capital = c("St John's", "Halifax", "Charlottetown", "Saint John"))

3.3.2 Create dataframe with multiple numeric columns

Description
Create a dataframe object in memory that has multiple numeric columns
Ingredients
Package Data
NA NA


Sample Instructions

dataframe_name <- data.frame(first_column_name = c(number, number, number, number), second_column_name = c(number, number, number, number))


Actual Instructions

weather_station_temperatures <- data.frame(station_id = c(100, 101, 102, 103), temperature = c(10, 15.1, 6.3, -4.3))

3.3.3 Create dataframe with multiple character and numeric columns

Description
Create a dataframe object in memory that has multiple character and numeric columns
Ingredients
Package Data
NA NA


Sample Instructions

dataframe_name <- data.frame(first_column_name = c(number, number, number, number), second_column_name = c("text", "text", "text", "text"), third_column_name = c(number, number, number, number), forth_column_name = c(number, number, number, number))


Actual Instructions

atlantic_capital_temperatures <- data.frame(station_id = c(100, 101, 102, 103), province = c("NL", "NS", "PE", "NB"), city = c("St John's", "Halifax", "Charlottetown", "Saint John"), temperature = c(10, 15.1, 6.3, -4.3))