R Tutorial
Fundamentals of R
Variables
Input and Output
Decision Making
Control Flow
Functions
Strings
Vectors
Lists
Arrays
Matrices
Factors
DataFrames
Object Oriented Programming
Error Handling
File Handling
Packages in R
Data Interfaces
Data Visualization
Statistics
Machine Learning with R
Objects are fundamental components in the R programming language. Everything you manipulate or create in R �� be it a number, vector, data frame, function, or plot �� is an object. Understanding objects and their properties is crucial to becoming proficient in R.
In this tutorial, we'll cover:
R supports various types of objects, each suited to different types of data or tasks. Some of the most common object types include:
Vectors: One-dimensional arrays that can hold numeric, character, or logical values.
numeric_vector <- c(1, 2, 3) character_vector <- c("A", "B", "C")
Matrices: Two-dimensional arrays that hold elements of the same type.
matrix_obj <- matrix(1:6, nrow=2, ncol=3)
Data Frames: Tabular structures similar to tables in databases or spreadsheets. Each column can be of different types.
df <- data.frame(Name = c("Alice", "Bob"), Age = c(25, 30))
Lists: Collections that can hold elements of different types, including other lists.
list_obj <- list(name="Alice", age=25, scores=c(95, 89, 78))
Factors: Vectors used to represent categorical data.
gender <- factor(c("Male", "Female", "Male"))
Functions: Objects that can take inputs and return outputs after performing certain operations.
my_function <- function(x) { return(x^2) }
You can inspect the properties of an object and modify them:
Structure: Use str()
to view the internal structure of an object.
str(df)
Class: Find out the type/class of an object using class()
.
class(character_vector)
Length: Determine the number of elements in an object with length()
.
length(numeric_vector)
Names: Get or set names for elements of vectors, lists, etc., using names()
.
names(list_obj)
Some attributes are associated with specific object types:
Dimensions: For matrices and arrays, dim()
provides or sets the dimensions.
dim(matrix_obj)
Row and Column Names: For matrices and data frames, rownames()
and colnames()
get or set the names of rows and columns, respectively.
rownames(df) colnames(df)
Levels: For factors, levels()
provides the levels/categories of the factor.
levels(gender)
Objects are the foundation of data storage, manipulation, and analysis in R. Recognizing different object types and understanding their properties allows you to leverage the full power of R's data handling capabilities. Whether you're doing simple data analysis or building complex models, a strong grasp of objects in R is essential.
Creating and manipulating objects in R:
# Creating a numeric vector object my_vector <- c(1, 2, 3, 4, 5) # Manipulating the object doubled_vector <- my_vector * 2
Data structures and types of objects in R:
# Examples of different data structures numeric_vector <- c(1, 2, 3) character_vector <- c("a", "b", "c") matrix_object <- matrix(1:6, nrow = 2, ncol = 3) data_frame_object <- data.frame(ID = c(1, 2, 3), Name = c("John", "Alice", "Bob"))
R code examples for working with objects:
# Indexing and modifying objects first_element <- my_vector[1] modified_vector <- my_vector + 10
Object-oriented programming in R:
# Example of creating a simple class setClass("Person", slots = list(name = "character", age = "numeric")) # Creating an instance of the class my_person <- new("Person", name = "Alice", age = 30)
Object storage and retrieval in R:
# Saving an object to a file saveRDS(my_vector, file = "my_vector.rds") # Retrieving the object from the file retrieved_vector <- readRDS("my_vector.rds")
Handling missing values in R objects:
is.na()
or by removing or imputing missing values.# Handling missing values missing_check <- is.na(my_vector) non_missing_vector <- my_vector[!missing_check]
Methods and functions for R objects:
# Example of using a method summary(data_frame_object)
Object-oriented vs. functional programming in R:
# Example of functional programming squared_values <- sapply(my_vector, function(x) x^2)