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.

Summary

Concept Description
Basic Data Types
Numeric Real numbers such as 2 or 15.5, the default numeric type in R
Integer Whole numbers written with a trailing L, for example 2L, to distinguish them from numeric
Character Strings of text enclosed in double quotes, for example "hello" or "1234"
Logical Boolean values TRUE and FALSE used for conditions and logical comparisons
Assigning and Operating
Assignment Operator Uses `<-` or `=` to bind a value to a name, with `<-` preferred for idiomatic R code
Comments Everything after the `#` symbol on a line is ignored by R, used to document intent
Arithmetic Operators `+`, `-`, `*`, `/`, and `^` apply element-wise to numbers, vectors, matrices, and arrays
Data Structures
Vector One-dimensional sequence of values of the same type, created using the `c()` combine function
Matrix Two-dimensional rectangular collection of same-type elements, created with the `matrix()` function and `nrow`, `ncol`, and `byrow` arguments
Array Extends the matrix idea to three or more dimensions, created with the `array()` function using `dim`