# As I delve into the world of R, I realize how pivotal vectors are in this programming language. I find myself fascinated by the various built-in constants that R offers, such as LETTERS for the uppercase Roman alphabet and letters for the lowercase. I notice these constants provide an efficient way for me to work with sequences of letters without the need to manually create them. It’s intriguing to see how I can quickly access specific letters by indexing into these constants.
# 
# One of my favorite discoveries is the sequence of months, both in their abbreviated and full forms. I appreciate how month.abb and month.name allow me to reference months without any hassle. It makes my work feel structured and organized, especially when I need to manipulate or reference specific months in my analysis.
# 
# Named vectors are another aspect that I find incredibly useful. I am able to assign names to the elements of a vector, which feels like giving labels to my data points for easy reference. When I use c() to create named vectors, it becomes a straightforward process. However, I also learn that using setNames() is just as effective and sometimes more intuitive. This feature feels like having a personal directory, where I can look up values by their names, enhancing my data retrieval process.
# 
# I also discover the simplicity and power of creating sequences of numbers using the colon : operator. It’s almost magical how a simple : can generate a range of numbers, whether ascending or descending. I understand that for more flexibility, like controlling the step size, the seq() function is my go-to tool. This function allows me to specify the start, end, and even the increment between numbers, which I find particularly useful when I need evenly spaced sequences for my plots or calculations.
# 
# Understanding how to work with different types of vectors also deepens my grasp of R. I learn about the various data types—integer, logical, character—and how they can be defined and manipulated. It’s fascinating to me that R can interpret a single integer or logical as a vector of size one. This understanding helps me appreciate how seamlessly R handles data, ensuring that I can combine or manipulate data without much manual intervention.
# 
# I find it quite empowering to know that I can manipulate elements within a vector by indexing. Whether I am retrieving a value or updating an element, this ability feels like wielding a powerful tool. The capability to subset vectors using specific indices or a range of indices gives me control over my data in ways that are both efficient and intuitive.
# 
# Lastly, the rep() function opens up new possibilities for me. It’s like having a tool that can clone parts of my data with precision. Whether I want to repeat an entire sequence or just specific elements, I see how rep() can make my tasks easier and faster. The option to repeat elements a specific number of times or create a pattern of repetitions feels like having a flexible data generation tool at my disposal.
# 
# As I teach myself these concepts, I notice how each function and feature of R builds on my previous knowledge, creating a cohesive understanding of vectors. It’s like assembling pieces of a puzzle, where each piece enhances the overall picture. Through this learning journey, I feel more confident in my ability to manipulate and understand data using R, knowing that these fundamental concepts will serve as the foundation for more complex operations in the future.
# Vectors from Built-in Constants

letters
##  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
## [20] "t" "u" "v" "w" "x" "y" "z"
LETTERS[3:5]
## [1] "C" "D" "E"
letters[c(4, 2, 1, 3, 5)]
## [1] "d" "b" "a" "c" "e"
month.abb
##  [1] "Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
month.name[5:8]
## [1] "May"    "June"   "July"   "August"
month.abb[c(1, 4, 7, 10)]
## [1] "Jan" "Apr" "Jul" "Oct"
# Creating Named Vectors

xc <- c('x' = 3, 'y' = 4, 'z' = 5, 'w' = 6)
xc
## x y z w 
## 3 4 5 6
xl <- list('x' = 3, 'y' = 4, 'z' = 5, 'w' = 6)
xl
## $x
## [1] 3
## 
## $y
## [1] 4
## 
## $z
## [1] 5
## 
## $w
## [1] 6
x <- 10:13
y <- LETTERS[24:27]
xy <- setNames(x, y)
xy
##    X    Y    Z <NA> 
##   10   11   12   13
xy <- 10:13
names(xy) <- LETTERS[24:27]
xy["Y"]
##  Y 
## 11
mydf <- data.frame(let = c('Z', 'X', 'Y', 'W'))
mydf$num <- xy[match(mydf$let, names(xy))]
mydf
##   let num
## 1   Z  12
## 2   X  10
## 3   Y  11
## 4   W  NA
# Sequence of Numbers

x <- 2:6
x
## [1] 2 3 4 5 6
8:3
## [1] 8 7 6 5 4 3
-2:2
## [1] -2 -1  0  1  2
# seq() Function

seq(6)
## [1] 1 2 3 4 5 6
seq(3, 7)
## [1] 3 4 5 6 7
seq(3, 7, 0.5)
## [1] 3.0 3.5 4.0 4.5 5.0 5.5 6.0 6.5 7.0
seq(3, 7, length.out = 5)
## [1] 3 4 5 6 7
seq(3, 7, along.with = 1:4)
## [1] 3.000000 4.333333 5.666667 7.000000
# Vectors

vector('numeric', 3)
## [1] 0 0 0
vector('logical', 3)
## [1] FALSE FALSE FALSE
integer(3)
## [1] 0 0 0
c(3, 4)
## [1] 3 4
c('x', 'y')
## [1] "x" "y"
c(FALSE, TRUE)
## [1] FALSE  TRUE
c(2, 2.2, 'z', FALSE)
## [1] "2"     "2.2"   "z"     "FALSE"
vec <- c(2, 4, 6)
vec[2]
## [1] 4
vec[2] <- 10
vec
## [1]  2 10  6
vec_char <- c('x', 'y', 'z', 'w')
vec_char[2:3]
## [1] "y" "z"
vec_char[c(1, 3, 4)]
## [1] "x" "z" "w"
# Expanding a Vector with rep()

rep(2:6, 3)
##  [1] 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6
rep(2:6, 3, length.out = 8)
## [1] 2 3 4 5 6 2 3 4
rep(2:6, each = 3)
##  [1] 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6
rep(2:6, c(2, 2, 1, 3, 1))
## [1] 2 2 3 3 4 5 5 5 6
rep.int(2:6, 3)
##  [1] 2 3 4 5 6 2 3 4 5 6 2 3 4 5 6
rep_len(2:6, length.out = 9)
## [1] 2 3 4 5 6 2 3 4 5
# Load necessary libraries
library(knitr)
library(kableExtra)

# Data for the table
data <- data.frame(
  Description = c(
    'Lowercase letters', 'Uppercase letters [3:5]', 'Custom letters',
    'Month abbreviations', 'Month names [5:8]', 'Selected month abbreviations',
    'Named vector (xc)', 'Named list (xl)', 'Set names (xy)', 'Set names (xy["Y"])',
    'Data frame with named vector lookup',
    'Sequence of numbers 2:6', 'Descending sequence 8:3', 'Negative sequence -2:2',
    'seq() from 1 to 6', 'seq() from 3 to 7', 'seq() from 3 to 7 by 0.5',
    'seq() with length.out', 'seq() with along.with',
    'Numeric vector', 'Logical vector', 'Integer vector', 'Character vector',
    'Mixed type vector', 'Retrieve second element', 'Modify second element',
    'Subset characters', 'Replicate vector 2:6 three times',
    'Replicate with length.out', 'Replicate with each', 'Custom replicate lengths',
    'rep.int()', 'rep_len()'
  ),
  Code = c(
    'letters', 'LETTERS[3:5]', 'letters[c(4, 2, 1, 3, 5)]',
    'month.abb', 'month.name[5:8]', 'month.abb[c(1, 4, 7, 10)]',
    "c('x' = 3, 'y' = 4, 'z' = 5, 'w' = 6)", "list('x' = 3, 'y' = 4, 'z' = 5, 'w' = 6)",
    "setNames(10:13, LETTERS[24:27])", "xy['Y']", "xy[match(mydf$let, names(xy))]",
    '2:6', '8:3', '-2:2', 'seq(6)', 'seq(3, 7)', 'seq(3, 7, 0.5)',
    'seq(3, 7, length.out = 5)', 'seq(3, 7, along.with = 1:4)',
    "vector('numeric', 3)", "vector('logical', 3)", "integer(3)", "c('x', 'y')",
    "c(2, 2.2, 'z', FALSE)", 'vec[2]', 'vec[2] <- 10', 'vec_char[2:3]',
    'rep(2:6, 3)', 'rep(2:6, 3, length.out = 8)', 'rep(2:6, each = 3)',
    'rep(2:6, c(2, 2, 1, 3, 1))', 'rep.int(2:6, 3)', 'rep_len(2:6, length.out = 9)'
  )
)

# Create a colorful table
kable(data, "html") %>%
  kable_styling("striped", full_width = F, position = "center", bootstrap_options = c("condensed", "hover", "responsive")) %>%
  row_spec(0, bold = TRUE, color = "white", background = "#4A90E2") %>%
  row_spec(1:nrow(data), background = rep(c("#F0F8FF", "#E6F7FF"), length.out = nrow(data))) %>%
  column_spec(1, bold = TRUE, color = "#333333") %>%
  column_spec(2, italic = TRUE, color = "#555555") %>%
  add_header_above(c(" " = 1, "Creating Vectors in R Studio Table" = 1), background = "#34495E", color = "white")
Creating Vectors in R Studio Table
Description Code
Lowercase letters letters
Uppercase letters [3:5] LETTERS[3:5]
Custom letters letters[c(4, 2, 1, 3, 5)]
Month abbreviations month.abb
Month names [5:8] month.name[5:8]
Selected month abbreviations month.abb[c(1, 4, 7, 10)]
Named vector (xc) c(‘x’ = 3, ‘y’ = 4, ‘z’ = 5, ‘w’ = 6)
Named list (xl) list(‘x’ = 3, ‘y’ = 4, ‘z’ = 5, ‘w’ = 6)
Set names (xy) setNames(10:13, LETTERS[24:27])
Set names (xy[“Y”]) xy[‘Y’]
Data frame with named vector lookup xy[match(mydf$let, names(xy))]
Sequence of numbers 2:6 2:6
Descending sequence 8:3 8:3
Negative sequence -2:2 -2:2
seq() from 1 to 6 seq(6)
seq() from 3 to 7 seq(3, 7)
seq() from 3 to 7 by 0.5 seq(3, 7, 0.5)
seq() with length.out seq(3, 7, length.out = 5)
seq() with along.with seq(3, 7, along.with = 1:4)
Numeric vector vector(‘numeric’, 3)
Logical vector vector(‘logical’, 3)
Integer vector integer(3)
Character vector c(‘x’, ‘y’)
Mixed type vector c(2, 2.2, ‘z’, FALSE)
Retrieve second element vec[2]
Modify second element vec[2] <- 10
Subset characters vec_char[2:3]
Replicate vector 2:6 three times rep(2:6, 3)
Replicate with length.out rep(2:6, 3, length.out = 8)
Replicate with each rep(2:6, each = 3)
Custom replicate lengths rep(2:6, c(2, 2, 1, 3, 1))
rep.int() rep.int(2:6, 3)
rep_len() rep_len(2:6, length.out = 9)