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
<- "text" object_name
Actual Instructions
<- "Halifax" character_variable
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
<- number object_name
Actual Instructions
<- 10 number_variable
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
<- number + number object_name
Actual Instructions
<- 10 + 10 total_variable
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
<- 5
first_number_variable <- 10 second_number_variable
Sample Instructions
<- variable1 + variable2 object_name
Actual Instructions
<- first_number_variable + second_number_variable combined_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
<- function() object_name
Actual Instructions
<- Sys.Date() current_date_variable
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
<- c("text", "text", "text") object_name
Actual Instructions
<- c("one", "two", "three") character_vector
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
<- c(number, number, number) object_name
Actual Instructions
<- c(1, 2, 3) number_vector
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
<- data.frame(first_column_name = c("text", "text", "text", "text"), second_column_name = c("text", "text", "text", "text")) dataframe_name
Actual Instructions
<- data.frame(province = c("NL", "NS", "PE", "NB"), capital = c("St John's", "Halifax", "Charlottetown", "Saint John")) atlantic_provinces
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
<- data.frame(first_column_name = c(number, number, number, number), second_column_name = c(number, number, number, number)) dataframe_name
Actual Instructions
<- data.frame(station_id = c(100, 101, 102, 103), temperature = c(10, 15.1, 6.3, -4.3)) weather_station_temperatures
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
<- 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)) dataframe_name
Actual Instructions
<- 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)) atlantic_capital_temperatures