# Introduction
# As I delved deeper into data analysis with R, I discovered the intricate world of data structures and classes. These foundational concepts shaped how I interacted with data, and my journey to understanding them has been both enlightening and practical. This journal documents my exploration of classes, vectors, and lists in R.
# Inspecting Classes
# One of the first steps I took was to inspect the class of different data objects. Understanding the class helped me determine how to handle and manipulate the data effectively.
class(Nile)
## [1] "ts"
# Using the `Nile` dataset, a time series object, I found its class to be `ts`. To dig deeper, I examined its structure.
str(Nile)
## Time-Series [1:100] from 1871 to 1970: 1120 1160 963 1210 1160 1160 813 1230 1370 1140 ...
# Time-Series
# This function provided insight into the time range and values in the series.
# Exploring more, I wanted to see the underlying numeric data and ensure its correct handling.
class(Nile[1])
## [1] "numeric"
# Vectors and Lists
# Vectors are fundamental in R, holding sequences of data with the same storage mode. I experimented with creating numeric, logical, and character vectors using the `c()` function.
numeric_vector <- c(4.5, 3.2, 7.8)
logical_vector <- c(TRUE, FALSE, TRUE)
character_vector <- c("delta", "alpha", "gamma")
# I learned that combining vectors is seamless with `c()`. For instance, merging two numeric vectors was straightforward.
vec1 <- c(3.14, 2.71, 1.62)
vec2 <- c(0.577, 1.414, 1.732)
combined_vector <- c(vec1, vec2)
# Lists in R offer flexibility by allowing different types of elements, including other lists. I found this particularly useful when functions needed to return multiple values.
complex_list <- list(Matrix = matrix(1:4, 2, 2), Text = "Complex", Nested = list(TRUE, 3.14))
# Lists also underpin data frames, which was an interesting discovery for me. Essentially, a data frame is a list of equal-length vectors.
list_to_df <- list(Col1 = 101:103, Col2 = c("A", "B", "C"))
my_df <- data.frame(list_to_df)
# I confirmed that `my_df` is indeed a list in disguise:
is.list(my_df)
## [1] TRUE
# Conclusion
# Reflecting on this journey, I’ve come to appreciate the power and versatility of R’s data structures. Understanding classes, vectors, and lists has transformed how I handle and interpret data. This foundational knowledge is crucial for efficient data analysis and manipulation.
# Load necessary libraries
library(knitr)
library(kableExtra)
# Create the data frame
data <- data.frame(
Function = c("class", "str", "c", "list", "data.frame"),
Purpose = c(
"Inspect the class of an object",
"Examine the structure of an object",
"Create a vector",
"Create a list with mixed types",
"Create a data frame"
),
Example = c(
"`class(Nile)`",
"`str(Nile)`",
"`c(3.14, 2.71, 1.62)`",
"`list(Matrix = matrix(1:4, 2, 2), Text = 'Complex')`",
"`data.frame(Col1 = 101:103, Col2 = c('A', 'B', 'C'))`"
)
)
# Generate the colorful table
kable(data, "html") %>%
kable_styling(bootstrap_options = c("striped", "hover", "condensed", "responsive"), full_width = FALSE) %>%
row_spec(0, bold = TRUE, color = "white", background = "#4d84c1") %>%
row_spec(1, background = "#DDEBF7") %>%
row_spec(2, background = "#F7DDDD") %>%
row_spec(3, background = "#DFF7DD") %>%
row_spec(4, background = "#DDEBF7") %>%
row_spec(5, background = "#F7DDDD")
|
Function
|
Purpose
|
Example
|
|
class
|
Inspect the class of an object
|
class(Nile)
|
|
str
|
Examine the structure of an object
|
str(Nile)
|
|
c
|
Create a vector
|
c(3.14, 2.71, 1.62)
|
|
list
|
Create a list with mixed types
|
list(Matrix = matrix(1:4, 2, 2), Text = 'Complex')
|
|
data.frame
|
Create a data frame
|
data.frame(Col1 = 101:103, Col2 = c('A', 'B', 'C'))
|