You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

Variables and assignment operators are the building blocks of R programming. They allow you to store and manipulate data, making your code more flexible and reusable. Understanding how to declare variables, assign values, and work with different data types is crucial for writing effective R programs.

R's object-oriented nature means everything is treated as an object, including variables. This concept, along with variable scoping rules, helps you organize and structure your code efficiently. Mastering these fundamentals sets the stage for more advanced R programming techniques.

Variable Declaration and Assignment

Naming Conventions and Data Types

Top images from around the web for Naming Conventions and Data Types
Top images from around the web for Naming Conventions and Data Types
  • Variables store data values in R programs
  • Created by assigning a value to a name using the assignment operator (
    [=](https://www.fiveableKeyTerm:=)
    or
    [<-](https://www.fiveableKeyTerm:<-)
    )
  • Variable names should follow conventions:
    • Start with a letter
    • Use only letters, numbers, and underscores
    • Avoid reserved keywords
    • Use descriptive names for readability (e.g.,
      age
      ,
      student_name
      )
  • Various data types can be assigned to variables:
    • : integer or floating-point numbers (e.g.,
      42
      ,
      3.14
      )
    • : text strings enclosed in quotes (e.g.,
      "Hello"
      ,
      'World'
      )
    • : boolean values
      TRUE
      or
      FALSE
    • Complex: numbers with real and imaginary parts (e.g.,
      2 + 3i
      )

Reassignment and Variable Manipulation

  • Variables can be reassigned to new values throughout the program execution
    • Allows for updating and modifying data as needed
    • Previous value is overwritten with the new assignment
  • Variables can be used in expressions and calculations
    • Mathematical operations: addition, subtraction, multiplication, division
    • Logical operations: comparison, AND, OR, NOT
    • Function arguments: passing variables as inputs to functions
  • Variables can be concatenated or combined to create new values
    • Joining character strings using the
      paste()
      function
    • Combining numeric values using arithmetic operators

Object-Oriented Programming in R

Objects and Attributes

  • R is an object-oriented programming language
  • Everything in R is treated as an object
  • Variables store and manipulate data objects
    • Each variable is associated with a specific data object
  • Objects have attributes that define their properties and behavior:
    • Class: determines the type and structure of the object (e.g.,
      numeric
      ,
      character
      ,
      factor
      )
    • Length: number of elements in the object (e.g., length of a )
    • Dimensions: shape of the object (e.g., number of rows and columns in a matrix)
  • Attributes can be accessed and modified using functions like
    class()
    ,
    length()
    ,
    dim()

Object Types and Manipulation

  • Variables can store various types of objects:
    • Vectors: one-dimensional sequence of elements of the same type (e.g.,
      c(1, 2, 3)
      ,
      c("a", "b", "c")
      )
    • Matrices: two-dimensional rectangular array of elements (e.g.,
      matrix(1:6, nrow = 2)
      )
    • Data frames: two-dimensional table-like structure with columns of different types (e.g.,
      data.frame(x = 1:3, y = c("a", "b", "c"))
      )
    • Lists: collection of objects of different types (e.g.,
      [list](https://www.fiveableKeyTerm:list)(1, "a", TRUE)
      )
    • Functions: reusable code blocks that perform specific tasks (e.g.,
      mean()
      ,
      sum()
      )
  • Objects can be passed as arguments to functions
    • Functions operate on the input objects and return results
    • Allows for modular and reusable code structure

Assignment Operators in R

Assigning Values to Variables

  • The assignment operator
    =
    is commonly used to assign values to variables
    • Assigns the value on the right side to the variable on the left side
    • Example:
      x = 10
      assigns the value
      10
      to the variable
      x
  • The arrow assignment operator
    <-
    is an alternative way to assign values
    • More explicit and visually distinguishes the assignment operation
    • Example:
      y <- "Hello"
      assigns the value
      "Hello"
      to the variable
      y
  • Both
    =
    and
    <-
    can be used interchangeably for assignment
    • <-
      is generally preferred for clarity and consistency

Multiple Assignments and Compound Operators

  • Multiple values can be assigned to multiple variables in a single line
    • Example:
      a = b <- c <- 5
      assigns the value
      5
      to variables
      a
      ,
      b
      , and
      c
  • Compound assignment operators combine assignment with an operation
    • +=
      : addition assignment (e.g.,
      x += 5
      is equivalent to
      x = x + 5
      )
    • -=
      : subtraction assignment (e.g.,
      y -= 2
      is equivalent to
      y = y - 2
      )
    • *=
      : multiplication assignment (e.g.,
      z *= 3
      is equivalent to
      z = z * 3
      )
    • /=
      : division assignment (e.g.,
      w /= 2
      is equivalent to
      w = w / 2
      )
  • Compound operators provide a concise way to update variables based on their current values

Variable Scoping in R

Lexical Scoping and Scope Hierarchy

  • Scoping rules determine the visibility and accessibility of variables within different parts of a program
  • R uses lexical scoping: the scope of a variable is determined by where it is defined in the code
  • Scope hierarchy in R:
    1. Local scope: variables defined within a specific function or code block
    2. Parent environments: variables defined in the enclosing environments of a function
    3. Global scope: variables defined in the global , accessible from anywhere in the program
  • When a variable is referenced, R looks for it in the local scope first, then in the parent environments, and finally in the global scope

Global and Local Variables

  • Global variables are defined in the global environment
    • Can be accessed from anywhere in the program
    • Created by assigning a value to a variable outside of any function
    • Example:
      global_var <- 10
      creates a
      global_var
      with value
      10
  • Local variables are defined within a specific function or code block
    • Only accessible within that limited scope
    • Created by assigning a value to a variable inside a function
    • Example:
      local_var <- 20
      inside a function creates a
      local_var
      with value
      20
  • Local variables take precedence over global variables with the same name
    • Allows for encapsulation and avoiding naming conflicts
  • It is recommended to use local variables within functions to improve code modularity and avoid unintended side effects
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary