Blog

  • Objects in R Programming

    Introduction to Objects in R

    In R, everything is an object. This is one of the most important concepts to understand when learning R programming. Variables, data structures, functions, models, and even expressions are treated as objects stored in memory.

    An object in R is a named container that holds data, attributes, and sometimes behavior.

    Examples of objects:

    • Numeric values
    • Vectors
    • Matrices
    • Data frames
    • Lists
    • Functions
    • Models (like linear regression results)

    What is an Object?

    An object is a data entity stored in memory that has:

    1. A value (data)
    2. A type (numeric, character, list, etc.)
    3. Attributes (optional metadata)
    4. A class (used in OOP)

    Example:

    x <- 10
    

    Here:

    • x is an object
    • 10 is the value
    • Type is numeric
    • Class is numeric

    Everything in R is an Object

    In R:

    • Numbers are objects
    • Strings are objects
    • Functions are objects
    • Data frames are objects

    Example:

    x <- 5
    y <- "R"
    f <- function(a) a + 1
    

    Check their classes:

    class(x)
    class(y)
    class(f)
    

    Creating Objects in R

    Objects are created using the assignment operator <- (preferred) or =.

    a <- 100
    b = "Data Science"
    

    Each assignment creates an object in memory.


    Naming Objects

    Object names follow the same rules as variable names.

    Rules:

    • Must start with a letter or .
    • Cannot start with a number
    • Case-sensitive
    • Cannot be reserved keywords

    Valid:

    score <- 95
    .myObject <- 10
    

    Invalid:

    1score <- 95
    

    Types of Objects in R

    Objects in R can be categorized based on the data they store.

    Atomic Objects

    These store a single type of data.

    • Numeric
    • Integer
    • Character
    • Logical
    • Complex

    Example:

    x <- 10
    y <- TRUE
    z <- "R"
    

    Composite Objects

    These store multiple values and structures.

    Object TypeDescription
    Vector1D, same data type
    Matrix2D vector
    ArrayMulti-dimensional
    ListDifferent data types
    Data FrameTabular data

    Example:

    v <- c(1, 2, 3)
    m <- matrix(1:6, nrow = 2)
    l <- list(1, "R", TRUE)
    df <- data.frame(id=1:3, name=c("A","B","C"))
    

    Object Attributes

    Attributes provide additional information about an object.

    Common attributes:

    • names
    • dim
    • class
    • levels

    Example:

    v <- c(a=10, b=20)
    attributes(v)
    

    Accessing Attributes

    Using attributes()

    attributes(v)
    

    Using attr()

    attr(v, "names")
    

    Modifying Attributes

    attr(v, "comment") <- "Score values"
    

    Class of an Object

    The class determines how an object behaves in functions.

    class(v)
    

    Change class manually:

    class(v) <- "myVector"
    

    This is commonly used in S3 OOP.


    Object Structure – str()

    The str() function displays the internal structure of an object.

    str(df)
    

    Output shows:

    • Type
    • Length
    • Attributes
    • Data preview

    Checking Object Properties

    Type of Object

    typeof(x)
    

    Class of Object

    class(x)
    

    Length of Object

    length(v)
    

    Dimensions of Object

    dim(m)
    

    Copying Objects in R

    R uses copy-on-modify behavior.

    x <- c(1,2,3)
    y <- x
    y[1] <- 100
    

    x remains unchanged until modification.


    Reference Objects (R6)

    Some objects (like R6) use reference semantics.

    library(R6)
    
    Counter <- R6Class("Counter",
      public = list(
        value = 0,
        inc = function() self$value <- self$value + 1
      )
    )
    
    c1 <- Counter$new()
    c2 <- c1
    c2$inc()
    c1$value   # changed
    

    Listing Objects in Memory

    Use ls() to see objects in the current environment.

    ls()
    

    Removing Objects from Memory

    Use rm() to delete objects.

    rm(x)
    

    Remove multiple objects:

    rm(a, b, c)
    

    Remove all objects:

    rm(list = ls())
    

    Object Environments

    Objects live inside environments.

    Common environments:

    • Global Environment
    • Local Environment
    • Package Environment

    Check current environment:

    environment()
    

    Objects and Functions

    Functions are also objects.

    f <- function(x) x * 2
    class(f)
    

    Functions can be:

    • Passed as arguments
    • Returned from other functions
    • Stored in lists

    Looping Over Objects

    Example with vector:

    v <- c(10, 20, 30)
    for (i in v) {
      print(i)
    }
    

    Example with list:

    l <- list(1, "R", TRUE)
    for (item in l) {
      print(item)
    }
    

    Practical Example

    student <- list(
      name = "Alice",
      marks = c(80, 85, 90)
    )
    
    average <- mean(student$marks)
    average
    

    Common Mistakes with Objects

    • Confusing type and class
    • Modifying reference objects unintentionally
    • Forgetting objects are copied on modification
    • Removing important objects accidentally using rm()

    Summary

    • Everything in R is an object
    • Objects store data, attributes, and class
    • Objects live in environments
    • Understanding objects is essential for OOP, data analysis, and package development
    • Proper object management improves performance and code clarity
  • Classes in R Programming

    Classes in detail

    Classes and Objects form the foundation of Object-Oriented Programming (OOP). They revolve around real-world entities. In R, everything is treated as an object. An object is essentially a data structure that contains attributes and methods. A class acts as a template or blueprint for creating objects, defining a set of properties or behaviors common to all objects of that type.

    Unlike many other programming languages, R features a three-class system: S3S4, and Reference Classes.

    Classes and Objects

    class is a template or blueprint from which objects are created by encapsulating data and methods. An object is a data structure containing attributes and methods that act upon those attributes.

    S3 Class

    The S3 class is the simplest and most flexible OOP system in R. It does not enforce a rigid structure or formal definitions. You can create an object of the S3 class by assigning a class attribute to it.

    Example:

    # Create a list with relevant attributes
    bookDetails <- list(title = "The Alchemist", author = "Paulo Coelho")
    
    # Assign a class name
    class(bookDetails) <- "book"
    
    bookDetails

    Output:

    $title
    [1] "The Alchemist"
    
    $author
    [1] "Paulo Coelho"

    In S3, methods are not tied to specific classes but to generic functions. For example, you can customize how the print function behaves for your objects.

    Example: Custom Print Method

    # Define a custom print method for class "book"
    print.book <- function(obj) {
        cat("Title:", obj$title, "\n")
        cat("Author:", obj$author, "\n")
    }
    
    # Print the object
    print(bookDetails)

    Output:

    Title: The Alchemist
    Author: Paulo Coelho
    S4 Class

    The S4 class offers a more structured approach to OOP in R, which programmers familiar with C++ or Java may find more intuitive. It provides formal class definitions, which include specified slots for storing data.

    Example:

    library(methods)
    
    # Define an S4 class
    setClass("novel", slots = list(title = "character", author = "character"))
    
    # Create an object of the S4 class
    novelDetails <- new("novel", title = "1984", author = "George Orwell")
    novelDetails

    Output:

    An object of class "novel"
    Slot "title":
    [1] "1984"
    
    Slot "author":
    [1] "George Orwell"

    S4 classes also allow you to define methods. Here’s an example:

    Example: Custom Method

    # Define a custom method for class "novel"
    setMethod("show", "novel",
              function(object) {
                  cat("Title:", object@title, "\n")
                  cat("Author:", object@author, "\n")
              })
    
    # Display the object
    novelDetails

    Output:

    # View attributes
    attributes(student)

    You can also define methods for Reference Classes:

    Example: Adding Methods

    libraryDetails <- setRefClass("library", fields = list(
        name = "character",
        location = "character",
        booksAvailable = "numeric"
    ), methods = list(
        addBooks = function(count) {
            booksAvailable <<- booksAvailable + count
        },
        removeBooks = function(count) {
            booksAvailable <<- booksAvailable - count
        }
    ))
    
    # Create an object and manipulate its fields
    libraryInfo <- libraryDetails(name = "Central Library", location = "Downtown", booksAvailable = 5000)
    
    # Print books available
    libraryInfo$booksAvailable
    
    # Add 200 books and print the count
    libraryInfo$addBooks(200)
    libraryInfo$booksAvailable
    
    # Remove 100 books and print the count
    libraryInfo$removeBooks(100)
    libraryInfo$booksAvailable

    Output:

    [1] 5000
    [1] 5200
    [1] 5100

  • Introduction to Object-Oriented Programming in R Language

    Introduction

    Object-Oriented Programming (OOP) is a programming paradigm that organizes software design around objects, rather than functions and logic alone. Objects represent real-world entities and combine data (attributes) and behavior (methods) into a single unit.

    In R, Object-Oriented Programming is especially important for:

    • Statistical modeling
    • Data analysis
    • Package development
    • Complex data structures
    • Reusable and maintainable code

    Unlike many languages (Java, C++), R supports multiple OOP systems, each designed for different use cases.


    What is Object-Oriented Programming?

    Object-Oriented Programming is based on four core principles:

    1. Encapsulation – bundling data and methods together
    2. Abstraction – exposing only essential details
    3. Inheritance – creating new objects from existing ones
    4. Polymorphism – same function behaving differently for different objects

    Why OOP is Important in R

    OOP helps:

    • Organize large programs
    • Reuse code efficiently
    • Write cleaner and modular code
    • Build extensible statistical models
    • Create professional R packages

    Many built-in R functions like plot(), print(), and summary() use OOP concepts internally.


    Object-Oriented Systems in R

    R supports three main OOP systems:

    1. S3 – Simple and informal (most common)
    2. S4 – Formal and strict
    3. R6 – Modern, reference-based OOP

    Objects in R

    An object in R is any data structure stored in memory.

    Examples:

    x <- 10
    y <- "R"
    z <- c(1, 2, 3)
    

    Everything in R is an object:

    • Variables
    • Vectors
    • Data frames
    • Functions
    • Models

    Classes in R

    A class defines the type or category of an object.

    class(x)
    

    Example:

    v <- c(1, 2, 3)
    class(v)
    

    Output:

    "numeric"
    

    Encapsulation in R

    Encapsulation means combining data and functions that operate on that data.

    In R:

    • Objects store data
    • Functions operate on objects

    Example:

    person <- list(name = "Alice", age = 25)
    

    Abstraction in R

    Abstraction means hiding implementation details and showing only essential functionality.

    Example:

    summary(lm(mpg ~ wt, data = mtcars))
    

    You don’t need to know how summary() works internally.


    Polymorphism in R

    Polymorphism allows the same function to behave differently depending on object type.

    Example:

    print(10)
    print("R")
    print(c(1, 2, 3))
    

    The print() function behaves differently for each object.


    Method Dispatch in R

    Method dispatch is how R decides which function to call for an object.

    Example:

    plot(1:10)
    plot(mtcars$wt, mtcars$mpg)
    

    Different plots, same function name.


    S3 Object-Oriented System (Introduction)

    What is S3?

    S3 is the simplest and most widely used OOP system in R.

    Characteristics:

    • Informal
    • No strict class definitions
    • Easy to use and flexible

    Creating an S3 Object

    person <- list(name = "Alice", age = 25)
    class(person) <- "Person"
    

    Creating an S3 Method

    print.Person <- function(obj) {
      cat("Name:", obj$name, "\n")
      cat("Age:", obj$age, "\n")
    }
    
    print(person)
    

    Polymorphism with S3

    print(10)
    print(person)
    

    Same function name, different behavior.


    S4 Object-Oriented System (Brief Introduction)

    What is S4?

    S4 is a formal OOP system with:

    • Explicit class definitions
    • Defined slots (attributes)
    • Strict type checking

    Example:

    setClass("Person",
             slots = list(
               name = "character",
               age = "numeric"
             ))
    

    R6 Object-Oriented System (Introduction)

    What is R6?

    R6 is a modern OOP system similar to Python and Java.

    Features:

    • Reference-based objects
    • Encapsulation
    • Private and public members

    Creating an R6 Class

    library(R6)
    
    Person <- R6Class(
      "Person",
      public = list(
        name = NULL,
        age = NULL,
        initialize = function(name, age) {
          self$name <- name
          self$age <- age
        },
        greet = function() {
          cat("Hello, my name is", self$name, "\n")
        }
      )
    )
    
    p <- Person$new("Alice", 25)
    p$greet()
    

    Inheritance in R6

    Student <- R6Class(
      "Student",
      inherit = Person,
      public = list(
        course = NULL,
        initialize = function(name, age, course) {
          super$initialize(name, age)
          self$course <- course
        }
      )
    )
    

    Comparison of OOP Systems in R

    FeatureS3S4R6
    FormalityLowHighHigh
    SyntaxSimpleVerboseModern
    InheritanceYesYesYes
    EncapsulationWeakStrongStrong
    Reference-basedNoNoYes

    When to Use Which OOP System

    • S3: Simple modeling, quick prototyping
    • S4: Complex statistical packages
    • R6: Applications, APIs, large systems

    Common Mistakes in OOP with R

    • Mixing S3 and S4 incorrectly
    • Not understanding method dispatch
    • Misusing reference-based objects
    • Overcomplicating simple tasks

    Summary

    R supports multiple object-oriented programming systems. OOP in R allows you to write reusable, modular, and maintainable code. Understanding S3, S4, and R6 is essential for advanced R programming, package development, and professional data science work.

  • Object Oriented Programming

    max() function in detail

    max() Function in R Language
    The max() function in R is used to identify the largest element in a given object. This object can be a vector, list, matrix, data frame, etc.

    Syntax

    max(object, na.rm)

    Parameters

    • object: Any R object such as a vector, matrix, list, or data frame.
    • na.rm: A logical value (TRUE or FALSE) that determines whether to ignore NA values.

    Example 1: Finding the Maximum Element in Vectors

    # Creating vectors
    vector1 <- c(12, 25, 8, 14, 19)
    vector2 <- c(5, NA, 17, 4, 10)
    
    # Finding the maximum element
    max(vector1)               # Without NA
    max(vector2, na.rm = FALSE) # Includes NA
    max(vector2, na.rm = TRUE)  # Excludes NA

    Output:

    [1] 25
    [1] NA
    [1] 17

    Example 2: Finding the Maximum Element in a Matrix

    # Creating a matrix
    matrix_data <- matrix(1:12, nrow = 3, ncol = 4)
    print(matrix_data)
    
    # Finding the maximum element
    max(matrix_data)

    Output:

    [,1] [,2] [,3] [,4]
    [1,]    1    4    7   10
    [2,]    2    5    8   11
    [3,]    3    6    9   12
    
    [1] 12

  • Print a Formatted string in R Programming – sprintf() Function

    sprintf() Function in detail

    The sprintf() function in R uses a format specified by the user to return the formatted string, inserting the values provided.

    Syntax:

    sprintf(format, values)

    Parameters:

    • format: The format of the string for printing values.
    • values: The variables or values to be inserted into the string.

    Example 1: Formatted String using sprintf()

    # R program to illustrate
    # the use of sprintf() function
    
    # Initializing values
    name <- "Alice"
    greeting <- "Good Morning"
    
    # Calling sprintf() function
    sprintf("%s, %s!", greeting, name)

    Output:

    [1] "Good Morning, Alice!"

    In this example, sprintf() is used to create a formatted string where the placeholders %s are replaced by the values of the variables greeting and name.

    Example 2: Formatted String with Numbers using sprintf()

    # R program to illustrate
    # the use of sprintf() function
    
    # Initializing values
    item <- "Apples"
    quantity <- 12
    price_per_item <- 2.5
    
    # Calling sprintf() function
    sprintf("The price for %d %s is $%.2f", quantity, item, quantity * price_per_item)

    Output:

    [1] "The price for 12 Apples is $30.00"

    This example demonstrates using sprintf() to format a string that includes both text and numeric values, where the %d placeholder is used for integers, and %.2f is used for floating-point numbers.

    Using paste(): The paste() function in R is used for concatenating elements into a single string, with optional separators between them.

    Example 3: Formatted String using paste()

    # Generate some example statistical results
    mean_value <- 35.68
    standard_deviation <- 7.42
    
    # Create a formatted string to display the results
    formatted_string <- paste("The mean is", round(mean_value, 2),
                              "and the standard deviation is", round(standard_deviation, 2))
    
    # Print the formatted string
    cat(formatted_string)

    Output:

    The mean is 35.68 and the standard deviation is 7.42

    Here, paste() is used to concatenate the components of the string, and round() ensures the values are formatted to two decimal places.

  • Splitting Strings in R programming – strsplit() method

    strsplit() method in detail

    The strsplit() function in R is used to divide a string into smaller parts based on a specified delimiter.

    Syntax of strsplit()

    strsplit(string, split, fixed)

    Parameters:

    • string: The input text or vector of strings.
    • split: The character or pattern used to split the string.
    • fixed: A logical value indicating whether the split should be treated as a literal match (TRUE) or as a regular expression (FALSE).

    Return Value:

    It returns a list containing the substrings obtained after the split.

    Examples of Splitting Strings in R

    Example 1: Using strsplit() with a Space as a Delimiter

    In this example, we use the strsplit() function to split a given text using space (" ") as a delimiter.

    # R program to split a string
    
    # Given String
    text <- "Data Science with R"
    
    # Using strsplit() method
    result <- strsplit(text, " ")
    
    print(result)

    Output:

    [[1]]
    [1] "Data"    "Science" "with"    "R"

    Example 2: Using Regular Expression to Split a String

    Here, we use a regular expression to split the string wherever one or more numeric characters ([0-9]+) appear.

    # R program to split a string using regex
    
    # Given String
    text <- "Learn7R5Programming"
    
    # Using strsplit() method
    result <- strsplit(text, split = "[0-9]+")
    
    print(result)

    Output:

    [[1]]
    [1] "Learn"      "R"          "Programming"

    Example 3: Splitting Date Strings Using strsplit()

    We can also split date strings into separate components using a specific delimiter, such as "-".

    # R program to split date strings
    
    # Given Date Strings
    date_strings <- c("10-05-2023", "15-06-2023", "20-07-2023", "25-08-2023", "30-09-2023")
    
    # Using strsplit() function
    result <- strsplit(date_strings, split = "-")
    
    print(result)

    Output:

    [[1]]
    [1] "10"  "05"  "2023"
    
    [[2]]
    [1] "15"  "06"  "2023"
    
    [[3]]
    [1] "20"  "07"  "2023"
    
    [[4]]
    [1] "25"  "08"  "2023"
    
    [[5]]
    [1] "30"  "09"  "2023"

  • Convert String from Uppercase to Lowercase in R programming – tolower() method

    tolower() Method in detail

    The tolower() function in R is used to convert uppercase letters in a string to lowercase.

    Syntax:

    tolower(s)

    Return: Returns the string in lowercase.

    Example 1:

    # R program to convert string
    # from uppercase to lowercase
    
    text <- "Hello World"
    
    # Using tolower() method
    result <- tolower(text)
    
    print(result)

    Output:

    [1] "hello world"

    Example 2: 

    # R program to convert string
    # from uppercase to lowercase
    
    # Given String
    sentence <- "ProGRamMing in R is FuN!"
    
    # Using tolower() method
    result <- tolower(sentence)
    
    print(result)

    Output:

    [1] "programming in r is fun!"
  • Convert string from lowercase to uppercase in R programming – toupper() function

    toupper() function in detail

    The toupper() function in R is used to convert a lowercase string to an uppercase string.

    Syntax:

    Return: Returns the uppercase version of the given string.

    Example 1:

    # R program to convert a string
    # from lowercase to uppercase
    
    # Given String
    text <- "Welcome to the world of R programming"
    
    # Using toupper() method
    result <- toupper(text)
    
    print(result)

    Output:

    [1] "WELCOME TO THE WORLD OF R PROGRAMMING"

    Example 2:

    # R program to convert a string
    # from lowercase to uppercase
    
    # Given String
    sentence <- "Practice makes a person perfect"
    
    # Using toupper() method
    converted <- toupper(sentence)
    
    print(converted)

    Output:

    [1] "PRACTICE MAKES A PERSON PERFECT"
  • Adding elements in a vector in R programming – append() method

    append() method in detail

    The append() function in R is used to insert values into a vector at a specified position. If no position is mentioned, the values are added at the end of the vector.

    Syntax:

    append(x, value, index(optional))

    Return Value: It returns a new vector with the specified values appended.

    Example 1: Appending a Value at the End of a Vector

    vec <- c(2, 4, 6, 8)
    
    # Appending value 12 to the vector
    result <- append(vec, 12)
    
    print(result)

    Output:

    [1]  2  4  6  8 12

    Example 2: Inserting a Value at a Specific Position

    vec <- c(5, 10, 15, 20)
    
    # Inserting 7 at the second position
    result <- append(vec, 7, after = 1)
    
    print(result)

    Output:

    [1]  5  7 10 15 20

  • Finding the length of string in R programming – nchar() method

    nchar() method in detail

    The nchar() function in R is used to determine the number of characters in a string object.

    Syntax:

    nchar(string)

    Return Value:

    The function returns the length (number of characters) present in the given string.

    Example 1: Finding the Length of a String

    In this example, we will calculate the length of a string using the nchar() function.

    # R program to determine the length of a string
    
    # Define a string
    text <- "Hello R Programming"
    
    # Use nchar() function
    length_result <- nchar(text)
    
    print(length_result)

    Output:

    [1] 20

    Example 2: Using nchar() with Character Vectors

    This example demonstrates how to apply nchar() to a vector containing different types of elements.

    # R program to get the length of character vectors
    
    # Defining a character vector
    vec <- c('code', '7', 'world', 99)
    
    # Displaying the type of vector
    typeof(vec)
    
    # Applying nchar() function
    nchar(vec)

    Output:

    'character'
    4 1 5 2

    Example 3: Handling NA Values in nchar()

    The nchar() function provides an optional argument keepNA, which helps when dealing with NA values.

    # R program to handle NA values using nchar()
    
    # Defining a vector with NULL and NA values
    vec <- c(NULL, '3', 'data', NA)
    
    # Applying nchar() with keepNA = FALSE
    nchar(vec, keepNA = FALSE)

    Output:

    1 4 2

    Here, NULL returns nothing, and NA is counted as 2 when keepNA = FALSE.

    If we set keepNA = TRUE, the output will be:

    # Applying nchar() with keepNA = TRUE
    vec <- c('', NULL, 'data', NA)
    
    nchar(vec, keepNA = TRUE)

    Output:

    0 4 <NA>

    This means that an empty string returns 0, and NA is explicitly shown as <NA> when keepNA = TRUE.