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

2.2 Variables and assignment

3 min readaugust 9, 2024

Variables and assignment are foundational concepts in R programming. They allow us to store and manipulate data efficiently, forming the building blocks of more complex operations and analyses.

Understanding variables and assignment is crucial for working with basic data types and objects in R. This knowledge enables us to create, modify, and organize data structures, setting the stage for more advanced programming techniques.

Variables and Assignment

Understanding Variables and Assignment in R

Top images from around the web for Understanding Variables and Assignment in R
Top images from around the web for Understanding Variables and Assignment in R
  • Variables serve as containers storing data values in R programming
  • Assignment operator
    [<-](https://www.fiveableKeyTerm:<-)
    assigns values to variables (can also use
    [=](https://www.fiveableKeyTerm:=)
    )
  • R uses lexical scoping rules determining how variable names are resolved
  • Variables follow specific naming conventions enhancing code readability
  • R treats uppercase and lowercase letters as distinct in variable names

Naming Conventions and Best Practices

  • Variable names start with a letter or period followed by letters, numbers, or underscores
  • Descriptive names improve code clarity (use
    total_sales
    instead of
    ts
    )
  • Avoid using reserved words as variable names (if, else, for, while)
  • Consistent naming style throughout the code enhances readability
  • Common naming conventions include (
    myVariable
    ) and (
    my_variable
    )

Working with Variables in R

  • Variables can store various data types (, , )
  • Use
    ls()
    function to all variables in the current environment
  • [rm()](https://www.fiveableKeyTerm:rm())
    function removes variables from the environment
  • Variables can be reassigned new values at any time
  • Use
    print()
    or simply type the variable name to display its contents

Data Types

Fundamental Data Types in R

  • Data types categorize different kinds of data in R programming
  • Numeric data type represents real numbers (includes both integers and floating-point numbers)
  • Character data type stores text or string values (enclosed in single or double quotes)
  • Logical data type represents boolean values (
    TRUE
    or
    FALSE
    )
  • Integer data type stores whole numbers (append L to ensure integer storage,
    42L
    )
  • Complex data type represents complex numbers (uses
    i
    as imaginary unit,
    3 + 2i
    )

Working with Numeric and Character Data

  • Numeric data supports mathematical operations (
    +
    ,
    -
    ,
    *
    ,
    /
    )
  • Use
    as.numeric()
    to convert other data types to numeric
  • Character data allows string manipulation (concatenation, substring extraction)
  • paste()
    function combines multiple strings into one
  • nchar()
    function counts the number of characters in a string

Logical and Special Data Types

  • Logical values result from comparisons (
    ==
    ,
    !=
    ,
    <
    ,
    >
    ,
    <=
    ,
    >=
    )
  • Logical operators include
    &
    (AND),
    |
    (OR), and
    !
    (NOT)
  • NA
    represents missing or undefined values in R
  • NULL
    indicates an empty object or uninitialized variable
  • Inf
    and
    -Inf
    represent positive and negative infinity

Data Structures

Vectors: Fundamental Data Structure

  • Vectors store elements of the same data type in a one-dimensional array
  • Created using
    c()
    function or sequence operators (
    :
    ,
    seq()
    )
  • Supports element-wise operations and functions
  • Indexing starts at 1 in R (unlike 0 in many other programming languages)
  • Use
    length()
    function to determine the number of elements in a

Lists and Data Frames

  • Lists store elements of different data types or structures
  • Created using
    list()
    function, allowing for nested structures
  • Data frames represent tabular data with rows and columns
  • Combine multiple vectors of equal length to form a using
    data.frame()
  • Access elements in lists and data frames using
    $
    operator or double square brackets
    [[]]

Matrices and Factors

  • Matrices store two-dimensional data of the same type
  • Created using
    matrix()
    function, specifying number of rows and columns
  • Factors represent categorical data with predefined levels
  • Use
    factor()
    function to create factors from character or numeric vectors
  • Factors optimize memory usage and are useful for statistical modeling
© 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