# R

## Data Types

R works with various data types:

• Numeric: This data type represents numerical values, including decimals. These values can be used for mathematical operations and calculations.
• Example: 3.14159 (pi), 100.5 (temperature)
• Integer: This data type represents whole numbers (no decimals). They are typically used for counting or representing discrete values.
• Example: 2024 (current year), 42 (the answer to the ultimate question of life, the universe, and everything)
• Logical: This data type represents Boolean values, which can be either TRUE or FALSE. They are often used in conditional statements or to represent results of comparisons.
• Example: TRUE (statement is correct), FALSE (statement is false)
• Also, T and F also mean true and false, respectively in R
• Character: This data type represents text strings. These can include letters, numbers, symbols, and punctuation. They are used to store any textual data.
• Example: "Hello, world!", "This is some text data"

These data types look something like this in code:

## Data Structures

Similarly, R also works with different data structures including

• Vectors: The most basic data structure in R. A vector is a one-dimensional ordered collection of elements, all of which must be of the same data type (numeric, character, etc.). It's like a simple list where elements are accessed by their position (index).
• Example: c(1, 3.14, "apple") (combines numeric, integer, and character elements)
• c()function stands for concatenate or combine
• Matrices: A two-dimensional array of elements, similar to a spreadsheet with rows and columns. Elements in a matrix must be of the same data type.
• Example: matrix(1:9, nrow = 3, ncol = 3) (creates a 3x3 matrix with numbers 1 to 9)
• Arrays: Similar to matrices, but arrays can have more than two dimensions (3D, 4D, etc.). They are less commonly used than vectors and matrices, but useful for storing complex multidimensional data. Elements must be of the same data type.
• Example: array(1:24, dim = c(2, 3, 4)) (creates a 2x3x4 array with numbers 1 to 24)
• Lists: Unlike vectors, lists can hold elements of different data types (numeric, character, logical, etc.) within a single structure. They are flexible for storing diverse data collections.
• Example: list(name = "Alice", age = 30, hobbies = c("reading", "hiking")) (combines character, numeric, and vector elements)
• Data Frames: A two-dimensional structure similar to a spreadsheet with rows and columns, but unlike matrices, data frames can hold elements of different data types in each column. This makes them ideal for storing tabular data where each row represents a record or observation.
• Example: data.frame(ID = 1:3, Name = c("Bob", "Charlie", "David"), Age = c(25, 32, 41)) (creates a data frame with numeric IDs, character names, and numeric ages)

These data structures look something like this in code:

## Checking Data Types and Structures

Understanding the data type is crucial for choosing appropriate functions to analyze or manipulate the data. Use functions like class()mode(), or typeof() to determine the data type, and functions like as.integer()as.vector(), or as.matrix() to convert between data types.

Conversion looks like this in code: