Category: R (programming language)

  • Data Structures

    data structure is a specific way of organizing and storing data in a computer so that it can be used efficiently. The goal is to optimize time and space complexity for various tasks. In R programming, data structures are tools designed to handle and manipulate collections of values.

    R’s native data structures are often categorized based on their dimensionality (1D, 2D, or nD) and whether they are homogeneous (all elements must be of the same type) or heterogeneous (elements can have different types). This classification results in six commonly used data structures for data analysis in R.

    The most essential data structures used in R include: 

    1. Vectors
    2. Lists
    3. Dataframes
    4. Matrices
    5. Arrays
    6. Factors
    7. Tibbles

    R Vectors

    Vectors in R are analogous to arrays in other programming languages and are used to store multiple values of the same type. A key difference is that R uses 1-based indexing (indexing starts at 1, not 0). Vectors can hold numeric, character, or logical values.

    Creating a Vector

    A vector is a fundamental data structure in R, representing a one-dimensional array. The c() function is the most common method for creating a vector.

    # R program to create Vectors
    
    # Using c() function to create a numeric vector
    A <- c(10, 20, 30, 40)
    cat('Using c() function:', A, '\n')
    
    # Using seq() function for generating a sequence
    # length.out defines the number of values in the sequence
    B <- seq(5, 25, length.out = 6)
    cat('Using seq() function:', B, '\n')
    
    # Using colon (:) to create a sequence
    C <- 3:9
    cat('Using colon operator:', C)

    Output:

    Using c() function: 10 20 30 40
    Using seq() function: 5 9 13 17 21 25
    Using colon operator: 3 4 5 6 7 8 9
    Types of R Vectors

    Numeric Vectors: Numeric vectors store numbers, which can be integers or doubles.

    # R program to create Numeric Vectors
    
    # Creating a vector using c()
    num1 <- c(1.5, 2.5, 3.5)
    
    # Display type of vector
    typeof(num1)
    
    # Using L to specify integers
    num2 <- c(10L, 20L, 30L)
    typeof(num2)

    Output:

    [1] "double"
    [1] "integer"

    Character Vectors: Character vectors store strings and can also include numbers treated as characters.

    # R program to create Character Vectors
    
    # Creating a character vector
    charVec <- c("apple", "orange", "42", "75")
    typeof(charVec)

    Output:

    [1] "character"

    Logical Vectors: Logical vectors store Boolean values (TRUEFALSE, or NA).

    # R program to create Logical Vectors
    
    # Creating a logical vector
    logVec <- c(TRUE, FALSE, TRUE, NA)
    typeof(logVec)

    Output:

    [1] "logical"
    Length of a Vector

    The length of a vector is the count of its elements. Use the length() function to find it.

    # R program to find vector lengths
    
    # Numeric vector
    numVec <- c(3, 6, 9, 12)
    length(numVec)
    
    # Character vector
    charVec <- c("car", "bike", "train")
    length(charVec)
    
    # Logical vector
    logVec <- c(TRUE, TRUE, FALSE, NA)
    length(logVec)

    Output:

    [1] 4
    [1] 3
    [1] 4
    Accessing Vector Elements

    Access elements using the subscript operator []. R uses 1-based indexing.

    # R program to access vector elements
    
    # Creating a vector
    vec <- c(8, 15, 23, 42, 57)
    
    # Accessing an element by index
    cat('Single element:', vec[3], '\n')
    
    # Accessing multiple elements using c()
    cat('Multiple elements:', vec[c(1, 5)], '\n')

    Output:

    Single element: 23
    Multiple elements: 8 57
    Modifying a Vector

    Example :

    # R program to modify a vector
    
    # Creating a vector
    vec <- c(5, 10, 15, 20)
    
    # Modify specific elements
    vec[2] <- 12
    vec[4] <- 25
    cat('Modified vector:', vec, '\n')
    
    # Modify using a range
    vec[1:3] <- c(7, 14, 21)
    cat('Modified using range:', vec, '\n')

    Output:

    Modified vector: 5 12 15 25
    Modified using range: 7 14 21 25
    Deleting a Vector

    Assign NULL to a vector to delete it.

    # R program to delete a vector
    
    # Creating a vector
    vec <- c(4, 8, 12, 16)
    
    # Deleting the vector
    vec <- NULL
    cat('Deleted vector:', vec)

    Output:

    Deleted vector: NULL
    Sorting a Vector
    # R program to sort a vector
    
    # Creating a vector
    vec <- c(9, 3, 7, 1, 5)
    
    # Sort in ascending order
    asc <- sort(vec)
    cat('Ascending order:', asc, '\n')
    
    # Sort in descending order
    desc <- sort(vec, decreasing = TRUE)
    cat('Descending order:', desc)

    Output:

    Ascending order: 1 3 5 7 9
    Descending order: 9 7 5 3 1

    R – List

    list in R is a generic object consisting of an ordered collection of objects. It is a one-dimensional, heterogeneous data structure, which means it can store different types of data, such as vectors, matrices, characters, and even functions.

    A list is essentially a vector but allows for elements of varying data types. You can create a list using the list() function, and its indexing starts from 1 instead of 0.

    Creating a List

    To create a list in R, you use the list() function. For example, let’s create a list to store details about books, such as their IDs, titles, and total count.

    Example:

    # R program to create a List
    
    # Numeric vector for book IDs
    bookId <- c(101, 102, 103, 104)
    
    # Character vector for book titles
    bookTitle <- c("R Programming", "Data Science", "Machine Learning", "AI")
    
    # Total number of books
    totalBooks <- 4
    
    # Combine all attributes into a list
    bookList <- list(bookId, bookTitle, totalBooks)
    
    print(bookList)

    Output:

    [[1]]
    [1] 101 102 103 104
    
    [[2]]
    [1] "R Programming"    "Data Science"      "Machine Learning" "AI"
    
    [[3]]
    [1] 4
    Naming List Components

    Assigning names to list components makes it easier to access them.

    Example:

    # Named list for a person’s details
    personDetails <- list(name = "Aarav", age = 30, city = "Mumbai")
    
    # Printing the named list
    print(personDetails)

    Output:

    $name
    [1] "Aarav"
    
    $age
    [1] 30
    
    $city
    [1] "Mumbai"
    Accessing R List Components

    1. Access Components by Names: Using the $ operator, you can directly access components by their names.

    Example:

    # List with named components
    bookList <- list(
      "ID" = c(101, 102, 103, 104),
      "Titles" = c("R Programming", "Data Science", "Machine Learning", "AI"),
      "Total Books" = 4
    )
    
    # Access Titles
    cat("Accessing titles using $ operator:\n")
    print(bookList$Titles)

    Output:

    Accessing titles using $ operator:
    [1] "R Programming"    "Data Science"      "Machine Learning" "AI"

    2. Access Components by Indices: You can also access components using indices. Use double brackets [[ ]] for top-level components and single brackets [ ] for inner components.

    Example:

    # Accessing by indices
    cat("Accessing titles using indices:\n")
    print(bookList[[2]])
    
    cat("Accessing a specific title using indices:\n")
    print(bookList[[2]][3])
    
    cat("Accessing the last book ID using indices:\n")
    print(bookList[[1]][4])

    Output:

    Accessing titles using indices:
    [1] "R Programming"    "Data Science"      "Machine Learning" "AI"
    
    Accessing a specific title using indices:
    [1] "Machine Learning"
    
    Accessing the last book ID using indices:
    [1] 104
    Modifying Components of a List

    You can modify the components of a list by accessing them and replacing their values.

    Example:

    # Modify components of a list
    cat("Before modifying the list:\n")
    print(bookList)
    
    # Modify total books count
    bookList$`Total Books` <- 5
    
    # Add a new book ID and title
    bookList[[1]][5] <- 105
    bookList[[2]][5] <- "Deep Learning"
    
    cat("After modifying the list:\n")
    print(bookList)

    Output:

    Before modifying the list:
    $ID
    [1] 101 102 103 104
    
    $Titles
    [1] "R Programming"    "Data Science"      "Machine Learning" "AI"
    
    $`Total Books`
    [1] 4
    
    After modifying the list:
    $ID
    [1] 101 102 103 104 105
    
    $Titles
    [1] "R Programming"    "Data Science"      "Machine Learning" "AI"               "Deep Learning"
    
    $`Total Books`
    [1] 5
    Concatenation of Lists

    You can concatenate two lists using the c() function.

    Example:

    # Original list
    bookList <- list(
      "ID" = c(101, 102, 103, 104),
      "Titles" = c("R Programming", "Data Science", "Machine Learning", "AI")
    )
    
    # New list with book prices
    bookPrices <- list("Prices" = c(500, 600, 700, 800))
    
    # Concatenate lists
    mergedList <- c(bookList, bookPrices)
    
    cat("After concatenating the lists:\n")
    print(mergedList)

    Output:

    $ID
    [1] 101 102 103 104
    
    $Titles
    [1] "R Programming"    "Data Science"      "Machine Learning" "AI"
    
    $Prices
    [1] 500 600 700 800
    Adding Items to a List

    You can append items to a list using the append() function.

    Example:

    # Original list
    myNumbers <- c(10, 20, 30)
    
    # Append a new number
    myNumbers <- append(myNumbers, 40)
    
    # Print updated list
    print(myNumbers)

    Output:

    [1] 10 20 30 40
    Deleting Components of a List

    To delete components, access them and use a negative index.

    Example:

    # List with named components
    bookList <- list(
      "ID" = c(101, 102, 103, 104),
      "Titles" = c("R Programming", "Data Science", "Machine Learning", "AI"),
      "Total Books" = 4
    )
    
    cat("Before deletion:\n")
    print(bookList)
    
    # Delete the "Total Books" component
    bookList <- bookList[-3]
    
    cat("After deleting 'Total Books':\n")
    print(bookList)

    Output:

    Before deletion:
    $ID
    [1] 101 102 103 104
    
    $Titles
    [1] "R Programming"    "Data Science"      "Machine Learning" "AI"
    
    $`Total Books`
    [1] 4
    
    After deleting 'Total Books':
    $ID
    [1] 101 102 103 104
    
    $Titles
    [1] "R Programming"    "Data Science"      "Machine Learning" "AI"

    R – Array

    Arrays are fundamental data storage structures defined with a specific number of dimensions. They are used to allocate space in contiguous memory locations.

    In R Programming, one-dimensional arrays are called vectors, where their single dimension is their length. Two-dimensional arrays are referred to as matrices, which consist of a defined number of rows and columns. Arrays in R hold elements of the same data type. Vectors serve as inputs to create arrays, specifying their dimensions.

    Creating an Array

    In R, arrays can be created using the array() function. The function takes a list of elements and dimensions as inputs to create the desired array.

    Syntax:

    array(data, dim = c(nrow, ncol, nmat), dimnames = names)

    Components:

    • nrow: Number of rows.
    • ncol: Number of columns.
    • nmat: Number of matrices with dimensions nrow * ncol.
    • dimnames: Defaults to NULL. Alternatively, a list can be provided containing names for each component of the array dimensions.
    Uni-Dimensional Array

    A vector, a one-dimensional array, has its length as its dimension. It can be created using the c() function.

    Example:

    vec <- c(10, 20, 30, 40, 50)
    print(vec)
    
    # Displaying the length of the vector
    cat("Length of the vector: ", length(vec))

    Output:

    [1] 10 20 30 40 50
    Length of the vector:  5
    Multi-Dimensional Array

    A matrix, or a two-dimensional array, is defined by rows and columns of the same data type. Matrices are created using the array() function.

    Example:

    # Create a matrix with values from 15 to 26
    mat <- array(15:26, dim = c(2, 3, 2))
    print(mat)

    Output:

    , , 1
         [,1] [,2] [,3]
    [1,]   15   17   19
    [2,]   16   18   20
    
    , , 2
         [,1] [,2] [,3]
    [1,]   21   23   25
    [2,]   22   24   26
    Naming Array Dimensions

    You can assign names to rows, columns, and matrices using vectors for better readability.

    Example:

    rows <- c("Row1", "Row2")
    columns <- c("Col1", "Col2", "Col3")
    matrices <- c("Matrix1", "Matrix2")
    
    named_array <- array(1:12, dim = c(2, 3, 2),
                         dimnames = list(rows, columns, matrices))
    print(named_array)

    Output:

    , , Matrix1
         Col1 Col2 Col3
    Row1    1    3    5
    Row2    2    4    6
    
    , , Matrix2
         Col1 Col2 Col3
    Row1    7    9   11
    Row2    8   10   12
    Accessing Arrays

    You can access elements of arrays using indices for each dimension. Names or positions can be used.

    Example:

    vec <- c(5, 10, 15, 20, 25)
    cat("Vector:", vec)
    cat("Second element:", vec[2])

    Output:

    Vector: 5 10 15 20 25
    Second element: 10
    Accessing Matrices in an Array

    Example:

    rows <- c("A", "B")
    columns <- c("X", "Y", "Z")
    matrices <- c("M1", "M2")
    
    multi_array <- array(1:12, dim = c(2, 3, 2),
                         dimnames = list(rows, columns, matrices))
    
    # Accessing first matrix
    print("Matrix M1")
    print(multi_array[, , "M1"])
    
    # Accessing second matrix by index
    print("Matrix 2")
    print(multi_array[, , 2])

    Output:

    Matrix M1
         X Y Z
    A    1 3 5
    B    2 4 6
    
    Matrix 2
         X Y Z
    A    7 9 11
    B    8 10 12
    Accessing Specific Rows and Columns

    Example:

    print("First row of Matrix 1")
    print(multi_array[1, , "M1"])
    
    print("Second column of Matrix 2")
    print(multi_array[, 2, 2])

    Output:

    First row of Matrix 1
    X Y Z
    1 3 5
    
    Second column of Matrix 2
    A 9
    B 10
    Modifying Arrays

    Adding Elements to Arrays: New elements can be added at specific positions or appended to the array.

    Example:

    vec <- c(1, 2, 3, 4)
    
    # Adding an element using c()
    vec <- c(vec, 5)
    print("After appending an element:")
    print(vec)
    
    # Using append() to add after the 2nd element
    vec <- append(vec, 10, after = 2)
    print("After using append:")
    print(vec)

    Output:

    After appending an element:
    [1] 1 2 3 4 5
    
    After using append:
    [1]  1  2 10  3  4  5

    Removing Elements

    Elements can be removed using logical conditions or indices.

    Example:

    vec <- c(1, 2, 3, 4, 5, 6)
    vec <- vec[vec != 4]  # Removing element with value 4
    print(vec)

    Output:

    [1] 1 2 3 5 6

    R – Matrices

    Matrices are two-dimensional, homogeneous data structures arranged in rows and columns.

    Creating a Matrix

    To create a matrix, use the matrix() function:

    matrix(data, nrow, ncol, byrow, dimnames)
    Parameters:
    • data: Elements to include.
    • nrow: Number of rows.
    • ncol: Number of columns.
    • byrow: Logical (TRUE for row-wise, FALSE for column-wise order).
    • dimnames: Names of rows and columns.

    Example:

    A <- matrix(c(1, 2, 3, 4, 5, 6, 7, 8, 9), nrow = 3, ncol = 3, byrow = TRUE)
    rownames(A) <- c("a", "b", "c")
    colnames(A) <- c("c", "d", "e")
    print(A)

    Output:

    c d e
    a 1 2 3
    b 4 5 6
    c 7 8 9
    Special Matrices

    1. Constant Matrix:

    matrix(5, 3, 3)

    2. Diagonal Matrix:

    diag(c(5, 3, 3), 3, 3)

    3. Identity Matrix:

    diag(1, 3, 3)
    Accessing Matrix Elements

    Use [row, col] notation:

    • Access rows: matrix[1:2, ]
    • Access columns: matrix[, 1:2]
    • Access specific element: matrix[1, 2]
    • Access submatrices: matrix[1:3, 1:2]
    Modifying Matrix Elements

    Direct assignment:

    Example:

    # Create a matrix
    matrix <- matrix(1:9, nrow = 3, ncol = 3)
    
    # Modify the element in the 3rd row and 3rd column
    matrix[3, 3] <- 30
    
    # Print the modified matrix
    print(matrix)

    Output:

    [,1] [,2] [,3]
    [1,]    1    4    7
    [2,]    2    5    8
    [3,]    3    6   30
    Matrix Concatenation

    Add a Row: You can add a row to the matrix using the rbind() function.

    Example:

    # Create a new row
    new_row <- c(10, 11, 12)
    
    # Add the new row to the matrix
    matrix <- rbind(matrix, new_row)
    
    # Print the updated matrix
    print(matrix)

    Output:

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

    Add a Column: You can add a column to the matrix using the cbind() function.

    # Create a new column
    new_col <- c(13, 14, 15, 16)
    
    # Add the new column to the matrix
    matrix <- cbind(matrix, new_col)
    
    # Print the updated matrix
    print(matrix)

    Output:

    [,1] [,2] [,3] [,4]
    [1,]    1    4    7   13
    [2,]    2    5    8   14
    [3,]    3    6   30   15
    [4,]   10   11   12   16
    Accessing Submatrices in R:

    You can access specific parts of a matrix (submatrices) using the colon : operator in R.

    Example:

    # R program to demonstrate accessing submatrices
    
    # Create a 4x4 matrix
    M = matrix(
      c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160),
      nrow = 4,
      ncol = 4,
      byrow = TRUE
    )
    cat("The 4x4 matrix:\n")
    print(M)
    
    cat("\nAccessing the first two rows and the first three columns:\n")
    print(M[1:2, 1:3])

    Output:

    The 4x4 matrix:
         [,1] [,2] [,3] [,4]
    [1,]   10   20   30   40
    [2,]   50   60   70   80
    [3,]   90  100  110  120
    [4,]  130  140  150  160
    
    Accessing the first two rows and the first three columns:
         [,1] [,2] [,3]
    [1,]   10   20   30
    [2,]   50   60   70
    Modifying Elements of an R-Matrix:

    You can modify elements in a matrix by directly assigning new values.

    Example:

    # R program to demonstrate modifying elements in a matrix
    
    # Create a 3x3 matrix
    M = matrix(
      c(2, 4, 6, 8, 10, 12, 14, 16, 18),
      nrow = 3,
      ncol = 3,
      byrow = TRUE
    )
    cat("Original matrix:\n")
    print(M)
    
    # Change the element in the 2nd row and 2nd column
    M[2, 2] = 100
    
    cat("\nMatrix after modification:\n")
    print(M)

    Output:

    Original matrix:
         [,1] [,2] [,3]
    [1,]    2    4    6
    [2,]    8   10   12
    [3,]   14   16   18
    
    Matrix after modification:
         [,1] [,2] [,3]
    [1,]    2    4    6
    [2,]    8  100   12
    [3,]   14   16   18
    Matrix Concatenation in R:

    Concatenation merges rows or columns of a matrix.

    Adding Rows Using rbind():

    # R program to demonstrate adding a row
    
    # Create a 2x3 matrix
    M = matrix(
      c(1, 2, 3, 4, 5, 6),
      nrow = 2,
      ncol = 3,
      byrow = TRUE
    )
    cat("Original matrix:\n")
    print(M)
    
    # Define a new row
    new_row = c(7, 8, 9)
    
    # Add the new row
    M_updated = rbind(M, new_row)
    
    cat("\nMatrix after adding a new row:\n")
    print(M_updated)

    Output:

    Original matrix:
         [,1] [,2] [,3]
    [1,]    1    2    3
    [2,]    4    5    6
    
    Matrix after adding a new row:
         [,1] [,2] [,3]
    [1,]    1    2    3
    [2,]    4    5    6
    [3,]    7    8    9

    Adding Columns Using cbind():

    Adding Columns Using cbind():
    R
    Copy
    Edit

    Output:

    Original matrix:
         [,1] [,2]
    [1,]   10   20
    [2,]   30   40
    
    Matrix after adding a new column:
         [,1] [,2] [,3]
    [1,]   10   20   50
    [2,]   30   40   60
    Deleting Rows and Columns:

    You can delete rows or columns by using negative indices.

    Deleting a Row:

    # R program to demonstrate row deletion
    
    # Create a 3x3 matrix
    M = matrix(
      c(5, 10, 15, 20, 25, 30, 35, 40, 45),
      nrow = 3,
      ncol = 3,
      byrow = TRUE
    )
    cat("Matrix before row deletion:\n")
    print(M)
    
    # Delete the 2nd row
    M_updated = M[-2, ]
    
    cat("\nMatrix after deleting the 2nd row:\n")
    print(M_updated)

    Output:

    Matrix before row deletion:
         [,1] [,2] [,3]
    [1,]    5   10   15
    [2,]   20   25   30
    [3,]   35   40   45
    
    Matrix after deleting the 2nd row:
         [,1] [,2] [,3]
    [1,]    5   10   15
    [2,]   35   40   45

    Deleting a Column:

    # R program to demonstrate column deletion
    
    # Create a 3x3 matrix
    M = matrix(
      c(2, 4, 6, 8, 10, 12, 14, 16, 18),
      nrow = 3,
      ncol = 3,
      byrow = TRUE
    )
    cat("Matrix before column deletion:\n")
    print(M)
    
    # Delete the 3rd column
    M_updated = M[, -3]
    
    cat("\nMatrix after deleting the 3rd column:\n")
    print(M_updated)

    Output:

    Matrix before column deletion:
         [,1] [,2] [,3]
    [1,]    2    4    6
    [2,]    8   10   12
    [3,]   14   16   18
    
    Matrix after deleting the 3rd column:
         [,1] [,2]
    [1,]    2    4
    [2,]    8   10
    [3,]   14   16
  • Input and Output

    Taking Input from User in R Programming

    Developers often need to interact with users to gather data or provide results. Most modern programs use dialog boxes or console input for this purpose. In R, it is also possible to take input from the user through two main methods:

    1. Using the readline() method
    2. Using the scan() method
    Using readline() Method

    The readline() method in R accepts input in string format. If a user inputs a number, such as 123, it is initially treated as a string ("123"). To use the input in other data types like integers, numeric, or dates, R provides functions to convert the input to the desired type.

    Conversion Functions in R:

    • as.integer(n) – Converts to integer
    • as.numeric(n) – Converts to numeric types (float, double, etc.)
    • as.complex(n) – Converts to complex numbers (e.g., 3+2i)
    • as.Date(n) – Converts to a date

    Syntax:

    var = readline();
    var = as.integer(var);
    # You can also use `<-` instead of `=`

    Example:

    # R program to demonstrate input using readline()
    
    # Taking input using readline()
    var = readline();
    
    # Convert the input to an integer
    var = as.integer(var);
    
    # Print the value
    print(var)

    Output:

    123
    [1] 123

    You can also display a message in the console to guide the user about what to input. Use the prompt argument inside the readline() function for this. The prompt argument is helpful for user interaction but is not mandatory.

    Syntax:

    var1 = readline(prompt = "Enter any number: ");
    or,
    var1 = readline("Enter any number: ");

    Example:

    # R program to demonstrate input with a prompt message
    
    # Taking input with a message
    var = readline(prompt = "Enter any number: ");
    
    # Convert the input to an integer
    var = as.integer(var);
    
    # Print the value
    print(var)

    Output:

    Enter any number: 123
    [1] 123
    Nomenclature Rules for R Variables
    1. Variable names can include alphabets, numbers, dot (.), and underscore (_).
      Example: var_1 is valid.
    2. No special characters (except dot and underscore) are allowed.
      Example: var$1 or var#1 is invalid.
    3. Variable names must start with alphabets or a dot (.).
      Example: my_var or .myVar is valid.
    4. Variable names must not begin with numbers or underscores.
      Example: 1var or _var is invalid.
    5. If a variable starts with a dot, the character immediately following cannot be a number.
      Example: .3var is invalid.
    6. Reserved keywords in R (e.g., TRUEFALSE) cannot be used as variable names.
    Important Functions for R Variables

    1. class() Function: This function identifies the data type of a variable.

    Syntax:

    class(variable)

    Example:

    number = 42
    print(class(number))

    Output:

    [1] "numeric"

    2. ls() Function: Lists all the variables in the current R workspace.

    Syntax:

    ls()

    Example:

    # Assigning values to variables
    name = "Alice"
    age <- 30
    height -> 5.5
    
    print(ls())

    Output:

    [1] "age" "height" "name"

    3. rm() Function: Deletes a specified variable from the workspace.

    Syntax:

    rm(variable)

    Example:

    # Assigning and removing a variable
    country = "India"
    rm(country)
    print(country)

    Output:

    Error in print(country): object 'country' not found
    Scope of Variables in R

    1. Global Variables: Accessible throughout the program and declared outside of any block or function.

    Example:

    # Declaring and accessing global variables
    global_var = 10
    
    display = function() {
      print(global_var)
    }
    
    display()
    
    # Modifying the global variable
    global_var = 20
    display()

    Output:

    [1] 10
    [1] 20

    2. Local Variables: Accessible only within the block or function where they are declared.

    Example:

    # Declaring and using a local variable
    my_function = function() {
      local_var = "This is local"
      print(local_var)
    }
    
    cat("Inside the function:\n")
    my_function()
    
    cat("Outside the function:\n")
    print(local_var)

    Output:

    Inside the function:
    [1] "This is local"
    Outside the function:
    Error in print(local_var): object 'local_var' not found
    Difference Between Local and Global Variables in R
    AspectGlobal VariablesLocal Variables
    ScopeAccessible throughout the program.Accessible only within the function/block.
    LifetimeExist until the program finishes execution or is removed explicitly.Exist only during the function/block execution.
    Naming ConflictsProne to conflicts as accessible globally.Limited scope minimizes conflicts.
    Memory UsageOccupies memory throughout the program’s execution.Utilized temporarily, thus conserving memory.

    Scope of Variable in R

    Variables in R are containers used to store data values. They serve as references, or pointers, to objects in memory. When a variable is assigned a value, it points to the memory location where the value is stored. Variables in R can hold vectors, collections of vectors, or combinations of various R objects.

    Example:

    # Assigning a value using the equal sign
    num_values = c(4, 5, 6, 7)
    print(num_values)
    
    # Assigning values using the leftward arrow
    languages <- c("JavaScript", "R")
    print(languages)
    
    # Assigning a vector
    numbers = c(10, 20, 30)
    print(numbers)
    
    names = c("Alice", "Bob", "Carol")
    print(names)
    
    # Creating a list of vectors
    combined_list = list(numbers, names)
    print(combined_list)

    Output:

    [1] 4 5 6 7
    [1] "JavaScript" "R"
    [1] 10 20 30
    [1] "Alice" "Bob" "Carol"
    [[1]]
    [1] 10 20 30
    
    [[2]]
    [1] "Alice" "Bob" "Carol"
    Naming Conventions for Variables

    Variable names in R must follow these rules:

    • They must start with an alphabet.
    • Only alphanumeric characters, underscores (_), and periods (.) are allowed.
    • Special characters such as !@#$ are not allowed.

    Example:

    # Valid variable names
    value1 = 15
    student_name = "John"
    course.Name = "Data Science"
    
    # Invalid variable names
    # 1value = 20
    # student@name = "John"

    Error:

    Error: unexpected symbol in "1value"
    Error: unexpected symbol in "student@name"
    Scope of Variables

    Global Variables: Global variables are accessible throughout the program. They are usually declared outside all functions and remain in memory until the program ends.

    Example:

    # Defining a global variable
    global_value = 42
    
    # Function accessing the global variable
    show_value = function() {
      print(global_value)
    }
    
    show_value()
    
    # Updating the global variable
    global_value = 100
    show_value()

    Output:

    [1] 42
    [1] 100

    Local Variables: Local variables exist only within the scope of the function or block in which they are defined. They are not accessible outside that block.

    Example:

    my_function = function() {
      # Defining a local variable
      local_value = 25
    }
    
    # Attempting to access the local variable
    print(local_value)

    Error:

    Error in print(local_value) : object 'local_value' not found

    To access the value, use print() within the function:

    my_function = function() {
      local_value = 25
      print(local_value)
    }
    
    my_function()

    Output:

    [1] 25

    Accessing Global Variables: Global variables are accessible across the program, but local variables are restricted to their scope.

    Example:

    set_value = function() {
      # Local variable
      local_var <- 15
    }
    
    set_value()
    print(local_var)

    Error:

    Error in print(local_var) : object 'local_var' not found

    To create or modify global variables inside a function, use the super assignment operator (<<-):

    set_global = function() {
      # Making a global variable
      global_var <<- 50
      print(global_var)
    }
    
    set_global()
    print(global_var)

    Output:

    [1] 50
    [1] 50

    Dynamic Scoping in R Programming

    R is an open-source programming language extensively used for statistical analysis and data visualization. It operates through a command-line interface and is compatible with popular platforms like Windows, Linux, and macOS. As a cutting-edge programming tool, R employs lexical scoping or static scoping as its primary mechanism for resolving free variables, distinguishing it from other languages that may use dynamic scoping.

    Dynamic Scoping: The Concept

    Consider the following function:

    h <- function(a, b) {
      a^2 + b/c
    }

    Here, the function h has two formal arguments a and b. Inside the function body, there is another symbol c, which is a free variable. Scoping rules determine how free variables like c are resolved. Free variables are neither formal arguments nor local variables assigned within the function.

    Lexical Scoping in R
    Under lexical scoping, the value of a free variable is resolved in the environment where the function was defined. If the variable’s value isn’t found in the defining environment, the search continues in its parent environment until the global environment or namespace is reached. If the variable remains undefined even in the global environment, an error is raised.

    Illustrative Example of Lexical Scoping
    create.power <- function(m) {
      power.calc <- function(num) {
        num ^ m
      }
      power.calc
    }
    
    cube <- create.power(3)
    square <- create.power(2)
    
    print(cube(4))   # Output: [1] 64
    print(square(5)) # Output: [1] 25

    Here, the function create.power generates a closure, where m is a free variable inside the nested power.calc function. The value of m is determined from the environment where create.power was defined.

    Inspecting Function Environments
    ls(environment(cube))
    # Output: [1] "m" "power.calc"
    
    get("m", environment(cube))
    # Output: [1] 3
    
    ls(environment(square))
    # Output: [1] "m" "power.calc"
    
    get("m", environment(square))
    # Output: [1] 2

    The environment() function retrieves the environment of a function, while ls() lists the variables in that environment. The get() function fetches the value of a specific variable within the environment.

    Comparing Lexical and Dynamic Scoping

    Let’s take another example to illustrate the difference:

    z <- 5
    
    f <- function(x) {
      z <- 3
      y <- function(p) {
        p + z
      }
      y(x)
    }
    
    y <- function(p) {
      p * z
    }

    Lexical Scoping: In this scenario, z in y(x) will resolve to 3, as it is found in the local environment of f.
    Dynamic Scoping: If dynamic scoping were used, z would resolve to 5, as it would be looked up in the calling environment.

    Output with Lexical Scoping:

    print(f(2))  # Output: [1] 5

    Example Demonstrating Errors with Free Variables

    y <- 4
    
    g <- function(x) {
      b <- 2
      x + b + y
    }
    
    print(g(3))  # Output: [1] 9

    If y is not defined in the environment, calling g(3) results in the following error:

    Error in g(3): object 'y' not found

    To fix this, ensure y has a value assigned before calling the function.

    Lexical Scoping in R Programming

    Lexical Scoping in R programming means that the values of the free variables are searched for in the environment in which the function was defined. An environment is a collection of symbols, value, and pair, every environment has a parent environment it is possible for an environment to have multiple children but the only environment without the parent is the empty environment. If the value of the symbol is not found in the environment in which the function was defined then the search is continued in the parent environment. In R, the free variable bindings are resolve by first looking in the environment in which the function was created. This is called Lexical Scoping.

    Why Lexical Scoping?

    Lexical scoping determines how R identifies the value of a variable based on the function’s definition rather than its execution context. This built-in behavior simplifies understanding variable resolution in R. Lexical scoping allows users to focus solely on the function’s definition, rather than its invocation, to predict how variables are resolved. This concept is especially beneficial in statistical computations.

    The term “lexical” in lexical scoping originates from the concept of “lexing” in computer science, which is the process of breaking down code into meaningful units that can be interpreted by a programming language.

    Example:

    f <- function(a, b) {
      a + b + c
    }
    
    c <- 5
    f(2, 3)

    Output:

    [1] 10

    In this case:

    • a and b are formal arguments.
    • c is a free variable. Its value is retrieved from the environment where the function was defined.
    Principles of Lexical Scoping

    There are four fundamental principles of lexical scoping in R:

    1. Name Masking
    2. Functions vs Variables
    3. A Fresh Start
    4. Dynamic Lookup

    1. Name Masking: When a variable’s name is not defined within a function, R looks for the variable in the parent environment.

    Example:

    x <- 15
    y <- function(a, b) {
      a + b + x
    }
    
    y(5, 10)

    Output:

    [1] 30

    Explanation: The function adds 5, 10, and the value of x from the global environment.

    If a variable is defined inside the function, it masks the variable from the parent environment.

    Example:

    x <- 20
    y <- function() {
      x <- 5
      x + 10
    }
    
    y()

    Output:

    [1] 15

    Explanation: The local definition of x within the function takes precedence.

    2. Functions vs Variables: R applies the same scoping rules to functions and variables.

    Example:

    f <- function(x) x * 2
    g <- function() {
      f <- function(x) x + 5
      f(3)
    }
    
    g()

    Output:

    [1] 8

    3. A Fresh Start: Every time a function is called, R creates a new environment, ensuring that the function’s execution is independent of previous calls.

    Example:

    a <- function() {
      if (!exists("y")) {
        y <- 10
      } else {
        y <- y + 10
      }
      y
    }
    
    a()

    Output:

    [1] 10

    4. Dynamic Lookup: Lexical scoping resolves where to look for variable values, but the lookup occurs at the time of function execution.

    Example:

    h <- function() z + 5
    z <- 10
    h()

    Output:

    [1] 15
    Identifying Global Variables

    The findGlobals() function from the codetools package identifies global variables used within a function. This can help in understanding external dependencies.

    Example:

    xGlobal <- runif(5)
    yGlobal <- runif(5)
    
    f <- function() {
      x <- xGlobal
      y <- yGlobal
      plot(y ~ x)
    }
    
    codetools::findGlobals(f)

    Output:

    [1] "{"       "~"       "<-"      "xGlobal" "yGlobal" "plot"
  • Variables

    R Variables – Creating, Naming and Using Variables in R

    A variable is a memory space allocated to store specific data. The name associated with the variable is used to interact with this reserved memory block. The name given to a variable is known as its variable name. Typically, a variable stores data of a specific data type. The term “variable” reflects its nature—values can vary during the program’s execution.

    Variables in R Programming

    R is a dynamically typed language. This means variables in R are not explicitly declared with a data type but inherit the data type of the object assigned to them.

    This characteristic is also seen in languages like Python and PHP.

    Creating Variables in R

    R supports three methods for assigning values to variables:

    1. Using the equal operator (=)
      Assigns values to variables directly.
    2. Using the leftward operator (<-)
      Copies data from right to left.
    3. Using the rightward operator (->)
      Copies data from left to right.

    Syntax for Creating Variables

    Equal Operator:

    variable_name = value

    Leftward Operator:

    variable_name <- value

    Rightward Operator:

    value -> variable_name

    Examples of Creating Variables in R

    R Program to Illustrate Variable Creation

    # Using equal operator
    fruit = "apple"
    print(fruit)
    
    # Using leftward operator
    city <- "Paris"
    print(city)
    
    # Using rightward operator
    "blue" -> color
    print(color)

    Output:

    [1] "apple"
    [1] "Paris"
    [1] "blue"
    Nomenclature Rules for R Variables
    1. Variable names can include alphabets, numbers, dot (.), and underscore (_).
      Example: var_1 is valid.
    2. No special characters (except dot and underscore) are allowed.
      Example: var$1 or var#1 is invalid.
    3. Variable names must start with alphabets or a dot (.).
      Example: my_var or .myVar is valid.
    4. Variable names must not begin with numbers or underscores.
      Example: 1var or _var is invalid.
    5. If a variable starts with a dot, the character immediately following cannot be a number.
      Example: .3var is invalid.
    6. Reserved keywords in R (e.g., TRUEFALSE) cannot be used as variable names.
    Important Functions for R Variables

    1. class() Function: This function identifies the data type of a variable.

    Syntax:

    class(variable)

    Example:

    number = 42
    print(class(number))

    Output:

    [1] "numeric"

    2. ls() Function: Lists all the variables in the current R workspace.

    Syntax:

    ls()

    Example:

    # Assigning values to variables
    name = "Alice"
    age <- 30
    height -> 5.5
    
    print(ls())

    Output:

    [1] "age" "height" "name"

    3. rm() Function: Deletes a specified variable from the workspace.

    Syntax:

    rm(variable)

    Example:

    # Assigning and removing a variable
    country = "India"
    rm(country)
    print(country)

    Output:

    Error in print(country): object 'country' not found
    Scope of Variables in R

    1. Global Variables: Accessible throughout the program and declared outside of any block or function.

    Example:

    # Declaring and accessing global variables
    global_var = 10
    
    display = function() {
      print(global_var)
    }
    
    display()
    
    # Modifying the global variable
    global_var = 20
    display()

    Output:

    [1] 10
    [1] 20

    2. Local Variables: Accessible only within the block or function where they are declared.

    Example:

    # Declaring and using a local variable
    my_function = function() {
      local_var = "This is local"
      print(local_var)
    }
    
    cat("Inside the function:\n")
    my_function()
    
    cat("Outside the function:\n")
    print(local_var)

    Output:

    Inside the function:
    [1] "This is local"
    Outside the function:
    Error in print(local_var): object 'local_var' not found

    Difference Between Local and Global Variables in R

    AspectGlobal VariablesLocal Variables
    ScopeAccessible throughout the program.Accessible only within the function/block.
    LifetimeExist until the program finishes execution or is removed explicitly.Exist only during the function/block execution.
    Naming ConflictsProne to conflicts as accessible globally.Limited scope minimizes conflicts.
    Memory UsageOccupies memory throughout the program’s execution.Utilized temporarily, thus conserving memory.

    Scope of Variable in R

    Variables in R are containers used to store data values. They serve as references, or pointers, to objects in memory. When a variable is assigned a value, it points to the memory location where the value is stored. Variables in R can hold vectors, collections of vectors, or combinations of various R objects.

    Example:

    # Assigning a value using the equal sign
    num_values = c(4, 5, 6, 7)
    print(num_values)
    
    # Assigning values using the leftward arrow
    languages <- c("JavaScript", "R")
    print(languages)
    
    # Assigning a vector
    numbers = c(10, 20, 30)
    print(numbers)
    
    names = c("Alice", "Bob", "Carol")
    print(names)
    
    # Creating a list of vectors
    combined_list = list(numbers, names)
    print(combined_list)

    Output:

    [1] 4 5 6 7
    [1] "JavaScript" "R"
    [1] 10 20 30
    [1] "Alice" "Bob" "Carol"
    [[1]]
    [1] 10 20 30
    
    [[2]]
    [1] "Alice" "Bob" "Carol"
    Naming Conventions for Variables

    Variable names in R must follow these rules:

    • They must start with an alphabet.
    • Only alphanumeric characters, underscores (_), and periods (.) are allowed.
    • Special characters such as !@#$ are not allowed.

    Example:

    # Valid variable names
    value1 = 15
    student_name = "John"
    course.Name = "Data Science"
    
    # Invalid variable names
    # 1value = 20
    # student@name = "John"

    Error:

    Error: unexpected symbol in "1value"
    Error: unexpected symbol in "student@name"
    Scope of Variables

    Global Variables: Global variables are accessible throughout the program. They are usually declared outside all functions and remain in memory until the program ends.

    Example:

    # Defining a global variable
    global_value = 42
    
    # Function accessing the global variable
    show_value = function() {
      print(global_value)
    }
    
    show_value()
    
    # Updating the global variable
    global_value = 100
    show_value()

    Output:

    [1] 42
    [1] 100

    Local Variables: Local variables exist only within the scope of the function or block in which they are defined. They are not accessible outside that block.

    Example:

    my_function = function() {
      # Defining a local variable
      local_value = 25
    }
    
    # Attempting to access the local variable
    print(local_value)

    Error:

    Error in print(local_value) : object 'local_value' not found

    To access the value, use print() within the function:

    my_function = function() {
      local_value = 25
      print(local_value)
    }
    
    my_function()

    Output:

    [1] 25

    Accessing Global Variables: Global variables are accessible across the program, but local variables are restricted to their scope.

    Example:

    set_value = function() {
      # Local variable
      local_var <- 15
    }
    
    set_value()
    print(local_var)

    Error:

    Error in print(local_var) : object 'local_var' not found

    To create or modify global variables inside a function, use the super assignment operator (<<-):

    set_global = function() {
      # Making a global variable
      global_var <<- 50
      print(global_var)
    }
    
    set_global()
    print(global_var)

    Output:

    [1] 50
    [1] 50

    Dynamic Scoping in R Programming

    R is an open-source programming language extensively used for statistical analysis and data visualization. It operates through a command-line interface and is compatible with popular platforms like Windows, Linux, and macOS. As a cutting-edge programming tool, R employs lexical scoping or static scoping as its primary mechanism for resolving free variables, distinguishing it from other languages that may use dynamic scoping.

    Dynamic Scoping: The Concept

    Consider the following function:

    h <- function(a, b) {
      a^2 + b/c
    }

    Here, the function h has two formal arguments a and b. Inside the function body, there is another symbol c, which is a free variable. Scoping rules determine how free variables like c are resolved. Free variables are neither formal arguments nor local variables assigned within the function.

    Lexical Scoping in R
    Under lexical scoping, the value of a free variable is resolved in the environment where the function was defined. If the variable’s value isn’t found in the defining environment, the search continues in its parent environment until the global environment or namespace is reached. If the variable remains undefined even in the global environment, an error is raised.

    Illustrative Example of Lexical Scoping
    create.power <- function(m) {
      power.calc <- function(num) {
        num ^ m
      }
      power.calc
    }
    
    cube <- create.power(3)
    square <- create.power(2)
    
    print(cube(4))   # Output: [1] 64
    print(square(5)) # Output: [1] 25

    Here, the function create.power generates a closure, where m is a free variable inside the nested power.calc function. The value of m is determined from the environment where create.power was defined.

    Inspecting Function Environments
    ls(environment(cube))
    # Output: [1] "m" "power.calc"
    
    get("m", environment(cube))
    # Output: [1] 3
    
    ls(environment(square))
    # Output: [1] "m" "power.calc"
    
    get("m", environment(square))
    # Output: [1] 2

    The environment() function retrieves the environment of a function, while ls() lists the variables in that environment. The get() function fetches the value of a specific variable within the environment.

    Comparing Lexical and Dynamic Scoping

    Let’s take another example to illustrate the difference:

    z <- 5
    
    f <- function(x) {
      z <- 3
      y <- function(p) {
        p + z
      }
      y(x)
    }
    
    y <- function(p) {
      p * z
    }

    Lexical Scoping: In this scenario, z in y(x) will resolve to 3, as it is found in the local environment of f.
    Dynamic Scoping: If dynamic scoping were used, z would resolve to 5, as it would be looked up in the calling environment.

    Output with Lexical Scoping:

    print(f(2))  # Output: [1] 5

    Example Demonstrating Errors with Free Variables

    y <- 4
    
    g <- function(x) {
      b <- 2
      x + b + y
    }
    
    print(g(3))  # Output: [1] 9

    If y is not defined in the environment, calling g(3) results in the following error:

    Error in g(3): object 'y' not found

    To fix this, ensure y has a value assigned before calling the function.

    Lexical Scoping in R Programming

    Lexical Scoping in R programming means that the values of the free variables are searched for in the environment in which the function was defined. An environment is a collection of symbols, value, and pair, every environment has a parent environment it is possible for an environment to have multiple children but the only environment without the parent is the empty environment. If the value of the symbol is not found in the environment in which the function was defined then the search is continued in the parent environment. In R, the free variable bindings are resolve by first looking in the environment in which the function was created. This is called Lexical Scoping.

    Why Lexical Scoping?

    Lexical scoping determines how R identifies the value of a variable based on the function’s definition rather than its execution context. This built-in behavior simplifies understanding variable resolution in R. Lexical scoping allows users to focus solely on the function’s definition, rather than its invocation, to predict how variables are resolved. This concept is especially beneficial in statistical computations.

    The term “lexical” in lexical scoping originates from the concept of “lexing” in computer science, which is the process of breaking down code into meaningful units that can be interpreted by a programming language.

    Example:

    f <- function(a, b) {
      a + b + c
    }
    
    c <- 5
    f(2, 3)

    Output:

    [1] 10

    In this case:

    • a and b are formal arguments.
    • c is a free variable. Its value is retrieved from the environment where the function was defined.
    Principles of Lexical Scoping

    There are four fundamental principles of lexical scoping in R:

    1. Name Masking
    2. Functions vs Variables
    3. A Fresh Start
    4. Dynamic Lookup

    1. Name Masking: When a variable’s name is not defined within a function, R looks for the variable in the parent environment.

    Example:

    x <- 15
    y <- function(a, b) {
      a + b + x
    }
    
    y(5, 10)

    Output:

    [1] 30

    Explanation: The function adds 5, 10, and the value of x from the global environment.

    If a variable is defined inside the function, it masks the variable from the parent environment.

    Example:

    x <- 20
    y <- function() {
      x <- 5
      x + 10
    }
    
    y()

    Output:

    [1] 15

    Explanation: The local definition of x within the function takes precedence.

    2. Functions vs Variables: R applies the same scoping rules to functions and variables.

    Example:

    f <- function(x) x * 2
    g <- function() {
      f <- function(x) x + 5
      f(3)
    }
    
    g()

    Output:

    [1] 8

    3. A Fresh Start: Every time a function is called, R creates a new environment, ensuring that the function’s execution is independent of previous calls.

    Example:

    a <- function() {
      if (!exists("y")) {
        y <- 10
      } else {
        y <- y + 10
      }
      y
    }
    
    a()

    Output:

    [1] 10

    4. Dynamic Lookup: Lexical scoping resolves where to look for variable values, but the lookup occurs at the time of function execution.

    Example:

    h <- function() z + 5
    z <- 10
    h()

    Output:

    [1] 15
    Identifying Global Variables

    The findGlobals() function from the codetools package identifies global variables used within a function. This can help in understanding external dependencies.

    Example:

    xGlobal <- runif(5)
    yGlobal <- runif(5)
    
    f <- function() {
      x <- xGlobal
      y <- yGlobal
      plot(y ~ x)
    }
    
    codetools::findGlobals(f)

    Output:

    [1] "{"       "~"       "<-"      "xGlobal" "yGlobal" "plot"
  • Fundamentals of R

    Basic Syntax

    R is a leading language for statistical computing and data analysis, supported by over 10,000 free packages in the CRAN repository. Like other programming languages, R has a specific syntax that is crucial to understand to leverage its powerful features.

    Before proceeding, ensure that R is installed on your system. We’ll use RStudio as our environment, though you can also use the R command prompt by executing the following command in your terminal:

    $ R
    Basic Hello World Program

    Type the following in your R console:

    cat("Hello, World!\n")

    Output:

    Hello, World!

    You can achieve the same result using print() as follows:

    print("Hello, World!")

    Typically, we write our code in scripts, saved as .R files. To execute a script, save the following code as helloWorld.R and run it in the console with:

    Rscript helloWorld.R

    Code:

    print("Hello, World!")

    Output:

    [1] "Hello, World!"
    Syntax of R Programs

    An R program consists of three primary components:

    1. Variables
    2. Comments
    3. Keywords
    Variables in R

    Previously, we directly printed output using print(). However, to reuse or manipulate data, we use variables. Variables act as named memory locations to store data. In R, variables can be assigned using three operators:

    • = (Simple Assignment)
    • <- (Leftward Assignment)
    • -> (Rightward Assignment)

    Example:

    # Simple Assignment
    a = 10
    print(a)
    
    # Leftward Assignment
    b <- 20
    print(b)
    
    # Rightward Assignment
    30 -> c
    print(c)

    Output:

    [1] 10
    [1] 20
    [1] 30

    While the rightward assignment operator is available, it’s less common and might confuse some developers. Therefore, it’s advisable to use = or <- for assigning values.

    Comments in R

    Comments are crucial for enhancing code readability and are ignored by the R interpreter.

    • Single-line Comments: Start with #.
    • Multi-line Comments: Though R does not natively support multi-line comments, you can achieve this effect using a trick, such as enclosing text within triple quotes inside if(FALSE) blocks.

    Example:

    # This is a single-line comment
    print("This is an example!")
    
    if (FALSE) {
      "This is a multi-line comment."
      "It will not be executed."
    }

    Output:

    [1] "This is an example!"
    Keywords in R

    Keywords are reserved words in R that hold a specific meaning and cannot be used as variable or function names.

    To view all reserved keywords, use one of the following commands in your R console:

    help("reserved")

    Or:

    ?reserved

    Commonly Used Keywords:

    • Control-flow and Function Declarationifelserepeatwhilefunctionforinnext, and break.
    • Boolean ConstantsTRUE and FALSE.
    • Special ValuesNaN (Not a Number), NULL (Undefined Value), and Inf (Infinity).

    Example:

    # Using control-flow
    if (TRUE) {
      print("Condition is TRUE!")
    }
    
    # Using a special value
    x <- Inf
    print(x)

    Output:

    [1] "Condition is TRUE!"
    [1] Inf

    R Comments

    Comments are plain English sentences that are added within a program to explain what the code does or what a specific piece of code is designed to achieve. These are intended for the programmer’s reference and have no impact on the actual logic or execution of the code.

    All programming languages use specific symbols to denote comments. When the compiler encounters these symbols, it identifies the text as a comment and ignores it during execution.

    Comments in R

    In the R programming language, comments are general statements written to describe the purpose or functionality of the code. They provide information for coders while having no effect on the logic or execution of the code, as comments are entirely ignored by the compiler.

    How Does the Compiler Recognize a Comment?

    The compiler identifies a comment based on the symbol used. In R, the # symbol is used to denote a comment.

    Uses of Comments

    Comments are generally used for the following purposes:

    1. Improving Code Readability
    2. Providing Explanations or Metadata
    3. Preventing Execution of Code
    4. Including Resources or Notes
    Types of Comments

    In programming, comments are typically categorized as:

    1. Single-line Comments: A comment that occupies only one line.
    2. Multi-line Comments: A comment that spans multiple lines.
    3. Documentation Comments: Comments that provide a detailed explanation or serve as a quick reference for the code’s functionality.
    Single-Line Comments in R

    Single-line comments are used to explain a single line of code. In R, any statement starting with # is treated as a comment.

    Syntax:

    # comment statement

    Example 1:

    # This is a comment

    The above line will not produce any output because the compiler will treat it as a comment and ignore it.

    Example 2:

    # R program to calculate the sum of two numbers
    
    # Assigning values to variables
    a <- 15
    b <- 5
    
    # Printing the sum
    print(a + b)

    Output:

    [1] 20
    Multi-line Comments in R

    As mentioned earlier, R does not have a built-in syntax for multi-line comments. However, multiple single-line comments can be used to achieve the same effect. R provides tools to make this process easier, especially in RStudio.

    Method 1: Using Keyboard Shortcut

    To comment on multiple lines at once, select the lines you wish to comment and press Ctrl + Shift + C (Windows/Linux) or Cmd + Shift + C (Mac). This will add or remove # at the beginning of the selected lines.

    Example:

    # This is a multi-line comment
    # Each line starts with the '#' symbol
    # The following code calculates and prints the sum
    
    x <- 25
    y <- 15
    sum <- x + y
    print(sum) # This line prints the value of 'sum'

    Output:

    [1] 40

    Method 2: Using RStudio GUI

    1. Highlight the lines you want to comment.
    2. Go to the Code menu in the toolbar.
    3. Select Comment/Uncomment Lines from the dropdown.

    R Operators

    Operators in R are symbols that instruct the compiler to execute specific operations between operands. They simulate mathematical, logical, and decision-based operations on integers, complex numbers, and vectors.

    Types of Operators in R
    1. Arithmetic Operators
    2. Logical Operators
    3. Relational Operators
    4. Assignment Operators
    5. Miscellaneous Operators
    1. Arithmetic Operators:

    Arithmetic operators are used for basic mathematical calculations and operate element-wise when used with vectors.

    Addition (+): Adds corresponding elements of two operands.

    x <- c(1.2, 0.5)
    y <- c(3.4, 1.5)
    print(x + y)

    Output:

    4.6  2.0

    Subtraction (-): Subtracts elements of the second operand from the first.

    x <- 10.5
    y <- 4.3
    print(x - y)

    Output:

    6.2

    Multiplication (*): Multiplies corresponding elements.

    a <- c(2, 3)
    b <- c(4, 5)
    print(a * b)

    Output:

    8  15

    Division (/): Divides the first operand by the second.

    a <- 9
    b <- 3
    print(a / b)

    Output:

    3

    Power (^): Raises the first operand to the power of the second.

    c

    Output:

    9

    Modulo (%%): Returns the remainder of division of the first operand by the second.

    x <- c(7, 12)
    y <- c(3, 5)
    print(x %% y)

    Output:

    1  2

    Example Code:

    x <- c(3, 7)
    y <- c(2, 5)
    cat("Addition:", x + y, "\n")
    cat("Subtraction:", x - y, "\n")
    cat("Multiplication:", x * y, "\n")
    cat("Division:", x / y, "\n")
    cat("Modulo:", x %% y, "\n")
    cat("Power:", x ^ y)

    Output:

    Addition: 5 12
    Subtraction: 1 2
    Multiplication: 6 35
    Division: 1.5 1.4
    Modulo: 1 2
    Power: 9 16807
    2. Logical Operators in R

    Logical operators in R are used for performing element-wise logical operations between values. These operators evaluate conditions and return a boolean result, either TRUE or FALSE. Any nonzero numeric value (real or complex) is treated as TRUE, whereas zero is considered FALSE.

    Element-wise Logical AND (&): This operator returns TRUE only if both elements being compared are TRUE.

    Example:

    val1 <- c(FALSE, 5)
    val2 <- c(3+2i, 0)
    print(val1 & val2)

    Output:

    FALSE  FALSE

    Element-wise Logical OR (|): This operator returns TRUE if at least one of the two elements is TRUE.

    Example:

    val1 <- c(FALSE, 5)
    val2 <- c(3+2i, 0)
    print(val1 | val2)
    TRUE  TRUE

    Logical NOT (!): A unary operator that reverses the logical value of each element.

    Example:

    val1 <- c(1, FALSE)
    print(!val1)
    FALSE  TRUE
    3. Relational Operators

    Relational operators compare elements of two operands and return boolean values.

    Less Than (<): Returns TRUE if elements of the first operand are less than the second.

    x <- c(2, 5)
    y <- c(3, 5)
    print(x < y)

    Output:

    TRUE FALSE

    Greater Than (>): Returns TRUE if elements of the first operand are greater than the second.

    x <- c(6, 4)
    y <- c(3, 4)
    print(x > y)

    Output:

    TRUE FALSE

    Equal To (==): Checks if elements are equal.

    x <- c(2, 4)
    y <- c(2, 5)
    print(x == y)

    Output:

    TRUE FALSE

    Example Code:

    x <- c(4, 7)
    y <- c(5, 7)
    cat("Less than:", x < y, "\n")
    cat("Greater than:", x > y, "\n")
    cat("Equal to:", x == y, "\n")

    Output:

    Less than: TRUE FALSE
    Greater than: FALSE FALSE
    Equal to: FALSE TRUE
    4. Assignment Operators

    Left Assignment (<-=): Assigns values to a variable.

    x <- c(1, 2, 3)
    y = c(4, 5, 6)
    print(x)
    print(y)

    Output:

    [1] 1 2 3
    [1] 4 5 6

    Right Assignment (->->>): Assigns values from left to right.

    c(7, 8, 9) -> z
    print(z)

    Output:

    [1] 7 8 9
    5. Miscellaneous Operators

    %in%: Checks if an element exists in a list or vector.

    x <- 3
    y <- c(1, 2, 3, 4)
    print(x %in% y)

    Output:

    TRUE

    %*%:Matrix multiplication operator.

    mat <- matrix(1:6, nrow = 2)
    print(mat)
    print(t(mat))
    result <- mat %*% t(mat)
    print(result)

    Output:

    [,1] [,2] [,3]
    [1,]    1    3    5
    [2,]    2    4    6
         [,1] [,2]
    [1,]    1    2
    [2,]    3    4
    [3,]    5    6
         [,1] [,2]
    [1,]   35   44
    [2,]   44   56

    R Keywords

    R is an open-source programming language extensively utilized for statistical computing and data analysis. It features a command-line interface and is supported on platforms such as Windows, Linux, and macOS. As a cutting-edge tool, R is highly regarded for its versatility.

    Keywords in R

    Keywords are reserved words in R with predefined functionalities. These words are critical for leveraging the capabilities of the R programming language. You can view the complete list of keywords in R using either help(reserved) or ?reserved. Below is a comprehensive list of keywords in R:

    if, else, while, repeat, for, function, in, next, break, TRUE, FALSE, NULL, Inf, NaN, NA, NA_integer_, NA_real_, NA_complex_, NA_character_, ...
    Key R Keywords with Examples

    1. if: The if statement evaluates a condition and executes a block of code if the condition is true.
    Example:

    # Check if a number is positive
    number <- 10
    if (number > 0) {
        cat("The number is positive\n")
    }

    Output:

    The number is positive
    The number is positive
    The number is positive

    2. else: The else statement executes a block of code if the if condition evaluates to false.
    Example:

    # Check if a number is positive or non-positive
    num <- -2
    if (num > 0) {
        cat(num, "is positive\n")
    } else {
        cat(num, "is not positive\n")
    }

    Output:

    -2 is not positive

    3. while: The while loop runs repeatedly until the condition becomes false.
    Example:

    # Print numbers from 1 to 4
    count <- 1
    while (count <= 4) {
        cat(count, "\n")
        count <- count + 1
    }

    Output:

    1
    2
    3
    4

    4. repeat: The repeat loop runs indefinitely until a break statement is encountered.
    Example:

    # Print numbers from 1 to 3 using repeat
    num <- 1
    repeat {
        cat(num, "\n")
        if (num == 3) {
            break
        }
        num <- num + 1
    }

    Output:

    1
    2
    3

    5. for: The for loop iterates over a sequence.
    Example:

    # Display square of numbers from 1 to 3
    for (val in 1:3) {
        cat("Square of", val, "is", val^2, "\n")
    }

    Output:

    Square of 1 is 1
    Square of 2 is 4
    Square of 3 is 9

    6. function: Functions allow you to encapsulate reusable code blocks.
    Example:

    # Function to calculate factorial
    factorial <- function(n) {
        if (n == 0) return(1)
        return(n * factorial(n - 1))
    }
    cat("Factorial of 5 is", factorial(5), "\n")

    Output:

    Factorial of 5 is 120

    7. next: The next statement skips the current iteration and proceeds to the next.
    Example:

    # Skip even numbers from 1 to 5
    for (i in 1:5) {
        if (i %% 2 == 0) next
        cat(i, "\n")
    }

    Output:

    1
    3
    5

    8. break: The break statement terminates a loop.
    Example: 

    # Terminate loop when i equals 3
    i <- 1
    while (TRUE) {
        cat(i, "\n")
        if (i == 3) break
        i <- i + 1
    }

    Output:

    1
    2
    3

    9. TRUE/FALSE: These keywords represent Boolean values.
    Example:

    # Logical comparison
    x <- 7
    y <- 5
    cat(x > y, "\n")  # TRUE
    cat(x < y, "\n")  # FALSE

    Output:

    TRUE
    FALSE

    10. NULL: The NULL keyword represents a null or undefined object.
    Example:

    # Example of NULL
    data <- NULL
    cat("Value of data is", data, "\n")

    Output:

    Value of data is NULL

    11. Inf and NaN: Inf and NaN represent infinity and “not a number,” respectively.
    Example:

    # Check for infinity and NaN
    nums <- c(1, Inf, NaN, 4)
    cat(is.finite(nums), "\n")  # Check finite
    cat(is.nan(nums), "\n")     # Check NaN

    Output:

    TRUE FALSE FALSE TRUE
    FALSE FALSE TRUE FALSE

    12. NA: NA represents missing values.
    Example:

    # Identify missing values
    vec <- c(10, NA, 20, 30)
    cat(is.na(vec), "\n")

    Output:

    FALSE TRUE FALSE FALSE

    R Data Types

    In programming, data types define and categorize the various forms of data that can be saved and manipulated. R programming provides a range of data types, each with unique properties and operations.

    What are R Data Types?

    R data types specify the kind of data a variable can store. Selecting the appropriate data type ensures efficient memory use and accurate computations. Variables in R do not need an explicitly declared data type and can have their types dynamically changed.

    Data Types in R Programming Language

    Every variable in R has a data type. These types dictate the memory requirements and the operations allowed.

    Common R Data Types:

    1. Numeric – Represents decimal values (e.g., 3.14121.5).
    2. Integer – Denotes whole numbers (e.g., 2L42L, where L declares integers).
    3. Logical – Boolean values (TRUEFALSE).
    4. Complex – Numbers with imaginary components (e.g., 7 + 5i).
    5. Character – Strings or text values (e.g., "Hello""123").
    6. Raw – Binary data represented at the byte level (e.g., as.raw(255)).

    1. Numeric Data Type: Decimal numbers are considered numeric in R. It is the default data type for numbers.

    # Example of Numeric Data Type
    x <- 7.8
    
    # Class and type
    print(class(x))     # Output: "numeric"
    print(typeof(x))    # Output: "double"

    Even assigning an integer to a variable will treat it as numeric:

    y <- 10
    print(class(y))      # Output: "numeric"
    print(typeof(y))     # Output: "double"
    print(is.integer(y)) # Output: FALSE

    2. Integer Data Type: Integer values are explicitly represented using the L suffix or as.integer() function.

    # Example of Integer Data Type
    a <- as.integer(25)
    b <- 50L
    
    # Class and type
    print(class(a))  # Output: "integer"
    print(typeof(a)) # Output: "integer"
    print(class(b))  # Output: "integer"

    3. Logical Data Type: Logical values represent boolean outcomes, such as TRUE or FALSE.

    # Example of Logical Data Type
    x <- 12
    y <- 8
    result <- x > y
    
    # Logical output
    print(result)     # Output: TRUE
    print(class(result)) # Output: "logical"

    4. Complex Data Type: Complex numbers have both real and imaginary parts.

    # Example of Complex Data Type
    c <- 3 + 4i
    
    # Class and type
    print(class(c))  # Output: "complex"
    print(typeof(c)) # Output: "complex"

    5. Character Data Type: Character strings can include text, symbols, or numbers treated as text.

    # Example of Character Data Type
    text <- "Data Science"
    
    # Class and type
    print(class(text))  # Output: "character"
    print(typeof(text)) # Output: "character"

    6. Raw Data Type: Raw data types are used for low-level binary operations.

    # Example of Raw Data Type
    raw_vec <- as.raw(c(0x10, 0x20, 0x30))
    print(raw_vec)  # Output: 10 20 30
    Checking Data Type in R

    Use the class() function to find an object’s data type:

    # Data type examples
    print(class(TRUE))         # Output: "logical"
    print(class(50L))          # Output: "integer"
    print(class(15.75))        # Output: "numeric"
    print(class(2+3i))         # Output: "complex"
    print(class("R Language")) # Output: "character"
    Coercing or Converting Data Types

    R allows converting one data type to another using as.<data_type>(). Note: Not all conversions are valid, and invalid ones return NA.

    # Data type conversion
    print(as.numeric(TRUE))     # Output: 1
    print(as.complex(10L))      # Output: 10+0i
    print(as.logical(0))        # Output: FALSE
    print(as.character(4+3i))   # Output: "4+3i"
    print(as.numeric("Text"))   # Output: NA

    Warning:

    Warning message:
    NAs introduced by coercion
  • Introduction of R

    Introduction

    The R Language stands out as a powerful tool in the modern era of statistical computing and data analysis. Widely embraced by statisticians, data scientists, and researchers, the R Language offers an extensive suite of packages and libraries tailored for data manipulation, statistical modeling, and visualization. This article explores the features, benefits, and applications of the R Programming Language, shedding light on why it has become an indispensable asset for data-driven professionals across various industries.

    R Programming Language Overview

    The R programming language is an implementation of the S programming language. It also combines lexical scoping semantics inspired by Scheme. Conceived in 1992, the project released its initial version in 1995, with a stable beta version introduced in 2000.

    What is R Programming Language?

    R programming is a leading tool for machine learning, statistics, and data analysis, allowing for the easy creation of objects, functions, and packages. Designed by Ross Ihaka and Robert Gentleman at the University of Auckland and developed by the R Development Core Team, R is platform-independent and open-source, making it accessible across all operating systems without licensing costs. Beyond its capabilities as a statistical package, R integrates with other languages like C and C++, facilitating interaction with various data sources and statistical tools. Originating as an implementation of the S programming language with influences from Scheme, R has evolved since its conception in 1992, with its first stable beta version released in 2000.

    Why Use R Language?

    The R Language is widely used for data analysis, statistical computing, and machine learning. Here are several reasons why professionals prefer R:

    1. Comprehensive Statistical Analysis: R provides a vast array of statistical techniques and tests, making it ideal for data-driven research.
    2. Extensive Packages and Libraries: Its ecosystem includes packages for advanced data manipulation, visualization, and machine learning tasks.
    3. Strong Data Visualization Capabilities: Tools like ggplot2 and plotly enable the creation of detailed and visually appealing graphs and plots.
    4. Open Source and Free: As an open-source language, R is accessible without costly licenses.
    5. Platform Independence: R runs on Windows, macOS, and Linux, offering flexibility.
    6. Integration with Other Languages: R interacts seamlessly with programming languages like C, C++, Python, and Java.
    7. Growing Community and Support: The active R community provides extensive support through forums, mailing lists, and online resources.
    8. High Demand in Data Science: R is one of the most in-demand programming languages in the Data Science job market.
    Features of R Programming Language

    Key features of R include:

    1. Comprehensive Statistical Techniques: R offers linear and nonlinear modeling, time-series analysis, classification, and clustering.
    2. Advanced Visualization: Packages like ggplot2 and lattice make complex visualizations possible.
    3. Extensive Libraries: CRAN hosts numerous packages for machine learning, data manipulation, and more.
    4. Platform Independence: R runs on multiple operating systems, ensuring ease of use.
    5. Integration with Languages: R integrates with Python, Java, SQL, and others.
    6. Interactive Development Environment (IDE): RStudio enhances productivity with its user-friendly interface.
    7. Reproducible Research Tools: R Markdown and Knitr enable dynamic reporting and reproducible research.
    Advantages of R Language
    • Provides comprehensive statistical techniques.
    • Open source and platform-independent.
    • A growing ecosystem of libraries and tools.
    • Ideal for statistical research and analysis.
    Disadvantages of R Language
    • Some packages may lack polish.
    • Memory management issues in large datasets.
    • Slower execution compared to languages like Python.
    Applications of R Language
    1. Data Science: R provides a range of libraries for statistical computing and data manipulation.
    2. Quantitative Analysis: Widely used by financial analysts for data importing and cleaning.
    3. Research and Academia: Preferred for data-driven studies due to its statistical tools.
    4. Industry Use: Companies like Netflix, Uber, and Airbnb rely on R for data analysis and insights.

    Fun Facts about R

    R is an open-source programming language widely recognized for its capabilities in statistical analysis and data visualization. It features a command-line interface and is compatible with multiple platforms, including Windows, Linux, and macOS. Developed as a cutting-edge tool, R was created by Ross Ihaka and Robert Gentleman at the University of Auckland, New Zealand. It continues to be maintained and enhanced by the R Development Core Team.

    Fascinating Facts About R Programming Language
    1. Origin and Naming:
      • R is an implementation of the S programming language, incorporating lexical scoping semantics inspired by Scheme.
      • The name “R” is derived from the first names of its creators, Ross and Robert, and is also a nod to the S programming language.
    2. Programming Paradigms:
      • R supports both procedural programming (featuring procedures, records, and modules) and object-oriented programming (with classes, objects, and generic functions).
    3. Interpreted Language:
      • As an interpreted language, R does not require a compiler to translate code into executable programs. This makes executing R scripts more efficient and less time-consuming.
    4. Extensive Package Ecosystem:
      • The R ecosystem includes over 100,000 packages available through repositories like CRAN or GitHub. These packages perform a wide range of tasks, from linear regression to machine learning applications.
    5. Popularity in Data Science:
      • R is among the fastest-growing languages in data science, second only to SQL in popularity. Approximately 70% of data professionals utilize R for tasks such as data mining and analysis.
    6. Reproducible Documents:
      • Using the rmarkdown package, R users can effortlessly create reproducible documents in formats like Word or PowerPoint by modifying a single line in the YAML header.
    7. Database Connectivity:
      • The dbplyr package allows seamless connection to nearly any database, enabling users to fetch and manipulate data directly. Additionally, tools like bigquery support integration with high-performance data storage solutions.
    8. Interactive Web Applications:
      • R empowers users to develop interactive web applications using the flexdashboard package. Hosting these apps is simplified with the rsconnect package, allowing deployment on personal or cloud servers.
    9. Creative Applications:
      • Beyond standard applications, R can be used for creative projects, such as building video game-like Shiny apps. The nessy package, for instance, allows developers to create apps reminiscent of retro NES (Nintendo Entertainment System) games.
    10. API Development:
      • With the plumber package, R functions can be converted into web APIs, enabling integration with other applications and systems.
    11. Global Recognition:
      • R ranks highly in programming language popularity metrics and is the leading choice for advanced analytics software searches. With a global user base exceeding 3 million, R has a thriving and supportive community.
    12. Open-Source and Free:
      • R is freely available for everyone and is widely used for statistical and graphical analysis.
    13. Rich Community Resources:
      • R has an active and resourceful user community, offering extensive documentation, forums, and tutorials for learners and practitioners alike.
    14. Applications Across Industries:
      • Industries like finance, healthcare, pharmaceuticals, and marketing leverage R for data modeling and analysis. For example, healthcare organizations use R for clinical trial data analysis, while marketers apply it to customer segmentation and predictive modeling.
    15. Academic Importance:
      • R is indispensable in research, with widespread usage in disciplines such as biology, psychology, and economics for tasks like gene expression analysis, behavioral studies, and econometric modeling.
    16. Platform Compatibility:
      • R operates seamlessly across various operating systems, including Windows, macOS, and Linux, ensuring accessibility for all users.

    Hello World in R Programming

    When learning a new programming language, it’s traditional to start with a “Hello World” program as a first step. This simple program introduces the basics of coding in the language.

    R programming is especially interesting because it allows us to achieve results with minimal code.

    Setting up R Programming

    Before you start coding, follow these steps to set up your environment:

    1. Visit the official R project website and download R for your operating system (Windows, Mac, or Linux).
    2. Install an IDE such as RStudio, RTVS, or StatET for writing and running R programs. Download RStudio here. Note: You must install R before installing any IDE.
    Writing the Hello World Program

    In R, creating a “Hello World” program is straightforward. All you need is the print() function. There’s no requirement for additional packages or a main function.

    Example 1: Basic Hello World

    print("Hello World")

    Output:

    [1] "Hello World"

    Additional Example

    Example 3: Printing Numeric Values

    print(12345)

    Output:

    [1] 12345
    Explanation of the print() Function

    The print() function is used to display text or values in the console. It takes various arguments to customize its behavior, such as:

    • x: The object to be printed (e.g., string, number).
    • quote: Logical value to control whether quotes are displayed around strings.
  • R – Lang Roadmap

    Introduction to R

    • Introduction
    • Fun Facts about R
    • Hello World in R Programming

    Fundamentals of R

    • Basic Syntax
    • R Comments
    • R Operators
    • R Keywords
    • R Data Types

    Variables

    • R Variables – Creating, Naming and Using Variables in R
    • Scope of Variable in R
    • Dynamic Scoping in R Programming
    • Lexical Scoping in R Programming

    Input and Output

    • Taking Input from User in R Programming
    • Scope of Variable in R
    • Dynamic Scoping in R Programming
    • Lexical Scoping in R Programming

    Data Structures

    • Data Structures in detail

    Vector

    • Introduction to Vectors
    • Operations on Vectors
    • Append Operation on Vectors
    • Dot Product of Vectors
    • Types of Vectors
    • Assigning Vectors 
    • Length of Vectors – length() Function
    • Creating a Vector of sequenced elements – seq() Function
    • Get Min and Max element of a Vector – range() Function
    • Formatting Numbers and Strings – format() Function
    • Replace the Elements of a Vector – replace() Function
    • Sorting of a Vector – sort() Function
    • Convert elements of a Vector to Strings – toString() Function
    • Extracting Substrings from a Character Vector – substring() Function

    Matrices

    • Introduction to Matrices
    • Create Matrix from Vectors
    • Operations on Matrices
    • Matrix Multiplication
    • Combining Matrices
    • Matrix Transpose
    • Inverse of Matrix
    • Working with Sparse Matrices
    • Check if the Object is a Matrix – is.matrix() Function
    • Convert an Object into a Matrix – as.matrix() Function
    • Get or Set Dimensions of a Matrix – dim() Function
    • Calculate Cumulative Sum of a Numeric Object – cumsum() Function
    • Compute the Sum of Rows of a Matrix or Array – rowSums Function

    Cloud Networking and Scalability

    • Two Dimensional List in R Programming

    Cloud Infrastructure and Architecture

    • Introduction to Factors
    • Level Ordering of Factors
    • Convert Factor to Numeric and Numeric to Factor
    • Check if a Factor is an Ordered Factor – is.ordered() Function
    • Checking if the Object is a Factor – is.factor() Function
    • Convert a Vector into Factor – as.factor() Function

    Control Flow

    • Control Statements
    • if, if-else, if-else-if ladder, nested if-else, and switch
    • For Loop
    • While Loop
    • Repeat loop
    • goto statement
    • Break and Next statements

    Control Flow

    • Introduction to Strings
    • Working with Text
    • String Manipulation
    • Concatenate Two Strings String Matching
    • How to find a SubString?
    • Finding the length of string – nchar() method
    • Adding elements in a vector – append() method
    • Convert string from Lowercase to Uppercase – toupper() function
    • Convert String from Uppercase to Lowercase – tolower() method
    • Splitting Strings – strsplit() method
    • Print a Formatted string – sprintf() Function

    Object Oriented Programming

    • Introduction to Object-Oriented Programming
    • Classes
    • Objects
    • Encapsulation
    • Polymorphism
    • Inheritance
    • Abstraction
    • Looping over Objects
    • Creating, Listing, and Deleting Objects in Memory
    • S3 class
    • Explicit Coercion
    • R6 Classes
    • Getting attributes of Objects – attributes() and attr() Function
    • Get or Set names of Elements of an Object – names() Function
    • Get the Minimum element of an Object – min() Function
    • Get the Maximum element of an Object – max() Function

    Error Handling

    • Handling Errors in R Programming
    • Condition Handling
    • Debugging in R Programming

    File Handling

    • Reading Files in R Programming
    • Writing to Files in R Programming
    • Working with Binary Files in R Programming

    Packages in R

    • Introduction to Packages
    • dplyr Package
    • ggplot2 package
    • Grid and Lattice Packages
    • Shiny Package
    • What Are the Tidyverse Packages?
    • Data Munging

    Data Interfaces

    • Data Handling
    • Importing Data in R Script
    • How To Import Data from a File?
    • Exporting Data from scripts 
    • Working with CSV files 
    • Working with XML Files
    • Working with Excel Files
    • Working with JSON Files 
    • Reading Tabular Data from files
    • Working with Databases
    • Database Connectivity
    • Manipulate Data Frames Using SQL

    Data Visualization

    • Graph Plotting
    • Graphical Models
    • Data Visualization
    • Charts and Graphs
    • Add Titles to a Graph
    • Adding Colors to Charts
    • Adding Text to Plots
    • Adding axis to a Plot
    • Set or View the
    • Graphics Palette
    • Plotting of Data using
    • Generic plots
    • Bar Charts
    • Line Graphs
    • Adding Straight Lines to a Plot
    • Addition of Lines to a Plot
    • Histograms
    • Pie Charts
    • Scatter plots
    • Create Dot Charts
    • Boxplots in R Language
    • Stratified Boxplot
    • Create a Heatmap
    • Pareto Chart
    • Waffle Chart
    • Quantile-Quantile Plot
    • Describe Parts of a Chart in Graphical Form
    • Principal Component Analysis

    Matrices

    • Introduction to Arrays
    • Multidimensional Array
    • Array Operations
    • Sorting of Arrays
    • Convert values of an Object to Logical Vector – as.logical() Function
    • Performing different Operations on Two Arrays – outer() Function
    • Get Exclusive Elements between Two Objects – setdiff() Function