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 in 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:

  1. Basic Object Types in R
  2. Inspecting and Modifying Objects
  3. Special Object Attributes

1. Basic Object Types in R

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) }
    

2. Inspecting and Modifying Objects

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)
    

3. Special Object Attributes

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)
    

Conclusion

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.

  1. Creating and manipulating objects in R:

    • In R, you can create objects by assigning values to variables. Objects can be vectors, matrices, data frames, lists, etc.
    # Creating a numeric vector object
    my_vector <- c(1, 2, 3, 4, 5)
    
    # Manipulating the object
    doubled_vector <- my_vector * 2
    
  2. Data structures and types of objects in R:

    • R supports various data structures, including vectors, matrices, data frames, lists, and factors.
    # 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"))
    
  3. R code examples for working with objects:

    • Various operations can be performed on objects, such as indexing, subsetting, and modifying.
    # Indexing and modifying objects
    first_element <- my_vector[1]
    modified_vector <- my_vector + 10
    
  4. Object-oriented programming in R:

    • Object-oriented programming (OOP) in R involves creating and manipulating objects using classes and methods.
    # 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)
    
  5. Object storage and retrieval in R:

    • Objects can be stored and retrieved using different methods, including saving to files or databases.
    # 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")
    
  6. Handling missing values in R objects:

    • Missing values in objects can be handled using functions like 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]
    
  7. Methods and functions for R objects:

    • Objects in R have associated functions and methods that can be applied to them.
    # Example of using a method
    summary(data_frame_object)
    
  8. Object-oriented vs. functional programming in R:

    • R supports both object-oriented and functional programming paradigms. Functional programming involves using functions as first-class citizens.
    # Example of functional programming
    squared_values <- sapply(my_vector, function(x) x^2)