4  Data Structures

4.1 Data Types in R

Data types refer to the kind of data that can be stored and manipulated within a program. In R, the basic data types include:

  • Numeric: Represents real numbers (e.g., 2, 15.5).
  • Integer: Represents whole numbers (e.g., 2L, where L denotes an integer).
  • Character: Represents strings (e.g., “hello”, “1234”). Character must be put between “.
  • Logical: Represents Boolean values (TRUE or FALSE).

4.1.1 Assigning Values and Basic Operations

Assignment Operator

  • The assignment operator in R is used to assign values to variables or objects in the R programming language.
  • The leftwards assignment operator <-: This is the most commonly used assignment operator in R. It assigns the value on its right to the object on its left. For example, x <- 3 assigns the value 3 to the variable x.
  • Alternative Assignment Operator (=) Apart from <-, R also supports the use of the = operator for assignments, similar to many other programming languages.
  • However, the use of <- is preferred in R for historical and readability reasons. For example, x = 3 is valid but x <- 3 is more idiomatic to R.

Use <- or = for assigning values, e.g., x <- 10 or x= 10

Commenting Code for Clarity

Use # for comments, e.g., # This is a comment.  - Comments are not executable and are used to provide relevant information about the syntax. Whatever is typed after # symbol, is considered as comment.

Arithmetic operators

  • In R, arithmetic operators are used to perform common mathematical operations on numbers, vectors, matrices, and arrays. Here’s an overview of the primary arithmetic operators available in R: +, -, *, /, ^

Division (/) operator - Divides the first number or vector by the second, element-wise.

Square (^) operator - Squares the first number by the second.


4.2 Data Structures

Vectors

  • Vectors are fundamental data structures that hold elements of the same type.
  • They are one-dimensional arrays that can store numeric, character, or logical data.
  • Assigning data to vectors in R is a basic operation, essential for data manipulation and analysis.
  • The c() function combines values into a vector. It’s the most common method for creating vectors.

Matrix

  • A two-dimensional, rectangular collection of elements of the same type.
  • All elements must be of the same data type.
  • Created using the matrix() function. nrow is used to set number of rows and byrow is used to set values by rows (if TRUE) or columns (if FALSE).

Array

  • Similar to matrices but can have more than two dimensions.
  • Elements within an array must all be of the same data type.
  • Created using the array() function. dimensions are set using dim.