R - Data Types
R - Data Types and Structures
Section titled “R - Data Types and Structures”In R, everything is an object. Variables are not declared with a specific data type; instead, they take on the type of the object assigned to them. Understanding the fundamental data types and the data structures that hold them is key to working effectively in R.
R has two main kinds of data objects: atomic vectors and lists. All other data structures, like matrices and data frames, are built on top of these.
Atomic Vector Types
Section titled “Atomic Vector Types”An atomic vector is the simplest R data structure and contains elements of the same type. There are six primary atomic types:
| Data Type | Description & Example | Check with class() |
|---|---|---|
| logical | Boolean values. TRUE, FALSE. | v <- TRUE print(class(v)) # [1] “logical” |
| numeric | Real numbers (floating-point). 12.3, 5, -99. | v <- 23.5 print(class(v)) # [1] “numeric” |
| integer | Integer numbers. Use an L suffix. 2L, 34L. | v <- 2L print(class(v)) # [1] “integer” |
| character | Text strings. "hello", 'R'. | v <- “R is fun” print(class(v)) # [1] “character” |
| complex | Complex numbers with an imaginary part. 3 + 2i. | v <- 2+5i print(class(v)) # [1] “complex” |
| raw | Holds raw bytes. Rarely used in data analysis. | v <- charToRaw(“Hi”) print(class(v)) # [1] “raw” |
Core Data Structures
Section titled “Core Data Structures”These structures are used to organize and store the atomic types.
Vectors
Section titled “Vectors”A vector is a one-dimensional sequence of data elements of the same type. Use the c() function (for combine) to create a vector with multiple elements.
# Create a character vectorfruit_colors <- c('red', 'green', 'yellow')print(fruit_colors)
# Get the class of the vectorprint(class(fruit_colors))[1] "red" "green" "yellow"[1] "character"A list is a generic vector that can contain elements of different types, including other lists, functions, or data frames.
# Create a list containing a numeric vector, a string, and a functionmy_list <- list(numbers = c(2, 5, 3), name = "My List", summary_func = summary)
# Print the listprint(my_list)$numbers[1] 2 5 3
$name[1] "My List"
$summary_funcfunction (object, ..., maxsum = 7, digits = max(3L, getOption("digits") - 3L), ...) .Primitive("summary")Matrices
Section titled “Matrices”A matrix is a two-dimensional rectangular data set. Like a vector, all its elements must be of the same type.
# Create a 2x3 matrix from a vectorM <- matrix(c(1:6), nrow = 2, ncol = 3)print(M) [,1] [,2] [,3][1,] 1 3 5[2,] 2 4 6Factors
Section titled “Factors”A factor is a special type of vector for storing categorical data. It stores the data as a vector of integer codes along with a corresponding set of character labels (levels). Factors are useful for statistical modeling and some plotting functions.
# Create a vector of educational levelseducation_levels <- c('High School', 'Bachelor', 'Bachelor', 'Master', 'PhD', 'Master')
# Create a factor objectfactor_education <- factor(education_levels)
# Print the factor and its levelsprint(factor_education)print(levels(factor_education))[1] High School Bachelor Bachelor Master PhD MasterLevels: Bachelor High School Master PhD[1] "Bachelor" "High School" "Master" "PhD"Data Frames and Tibbles
Section titled “Data Frames and Tibbles”Data frames are the most common way to store tabular data in R. They are essentially a list of vectors of equal length, meaning each column can have a different data type. The tibble is a modern reimagining of the data frame, offered by the tidyverse. Tibbles are better for modern data science because they are stricter and have more convenient printing methods.
# Load the tidyverse package (or just tibble)# install.packages("tidyverse")library(tidyverse)
# Create a tibble (a modern data frame)patient_data <- tibble( gender = c("Male", "Male", "Female"), height_cm = c(152, 171.5, 165), weight_kg = c(81, 93, 78), age = c(42L, 38L, 26L) # Use L for integers)
print(patient_data)Notice how the tibble neatly prints the data types of each column:
# A tibble: 3 × 4 gender height_cm weight_kg age <chr> <dbl> <dbl> <int>1 Male 152 81 422 Male 171. 93 383 Female 165 78 26