What is vector?

Vectors are one of the most fundamental data structures in R. They are essentially ordered collections of elements, where all elements must be of the same data type.

In R, the c() function is a fundamental tool used to combine values into a vector. A vector is a one-dimensional array that can hold elements of the same data type. Combining series of elements in a vector


Vector using c() function

In R, the c() function is a fundamental tool used to combine values into a vector. A vector is a one-dimensional array that can hold elements of the same data type.

# Declared variable of different types using vector
my_number <- c(1, 10, 15)
print(my_number)
## [1]  1 10 15
my_character <- c("z", "x", "y")
print(my_character, str(my_character))
##  chr [1:3] "z" "x" "y"
## [1] "z" "x" "y"
my_logical <- c(FALSE, TRUE, FALSE)
print(my_logical)
## [1] FALSE  TRUE FALSE

Names

The names() function is used to get or set the names of elements within an object, such as a vector, list, or data frame.

Setting Names

To assign names to an object, you use the assignment operator <- with the names() function:

# Unnamed vector as daily_sales_2
daily_sales <- c(100, 250, 75, 15, 20, 90, 120)

# Assign names Monday to Sunday to unnamed vector daily_sales
names(daily_sales) <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

# Print result
print(daily_sales)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##       100       250        75        15        20        90       120

You can also assign names directly when creating a vector using = (equal) sign

# Unnamed vector as daily_sales
daily_sales_2 <- c("Monday" = 100, "Tuesday" = 250, "Wednesday" = 75, "Thursday" = 15, "Friday" = 20, "Saturday" = 90, "Sunday" = 120)

# Print result
print(daily_sales_2)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##       100       250        75        15        20        90       120

Using = equal sign assigning the variable

Getting Names

To retrieve the names of an object, you simply use the names() function:

names(daily_sales)
## [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"    "Saturday" 
## [7] "Sunday"

Setting names using variables

# Number of fingers in sequence
finger_number <- c(1, 2, 3, 4, 5)
# Names of fingers from Thumb to Pinky
fingers_names <- c("Thumb", "Index", "Middle", "Ring", "Pinky")

# Assign finger_numbers to finger_names
names(finger_number) <- fingers_names

print(finger_number)
##  Thumb  Index Middle   Ring  Pinky 
##      1      2      3      4      5

Vectors with Math Operator

# Create a vector of sales_burger
sales_burger <- c(100, 250, 75, 15, 20, 120, 150)

# Create a vector of sales_lemonade
sales_lemonade <- c(20, 30, 10, 25, 20, 50, 70)

# Calculate the sum of sales_burger and sales_lemonade
total_sales <- sales_burger + sales_lemonade
# Create a vector of days
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

# Assign names
names(total_sales) <- days_vector

# Print the total_sales with days_vector
print(total_sales)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##       120       280        85        40        40       170       220

Vector using sum() function

# Create a vector of sales_burger
sales_burger <- c(100, 250, 75, 15, 20, 120, 150)
names(sales_burger) <- days_vector
print(sales_burger)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##       100       250        75        15        20       120       150
# Create a vector of sales_lemonade
sales_lemonade <- c(20, 30, 10, 25, 20, 50, 70)
names(sales_lemonade) <- days_vector
print(sales_lemonade)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##        20        30        10        25        20        50        70
# Create a vector of days
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

# Total sales per day
sales_per_day <- sales_burger + sales_lemonade
print(sales_per_day)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##       120       280        85        40        40       170       220
# Calculate the sum of sales_burger and sales_lemonade
sum_sales_burger <- sum(sales_burger)
print(sum_sales_burger)
## [1] 730
sum_sales_lemonade <- sum(sales_lemonade)
print(sum_sales_lemonade)
## [1] 225
# Calculate the total_sales_in_a_week
total_sales <- sum(sum_sales_burger, sum_sales_lemonade)

# Print the total_sales with days_vector
print(total_sales)
## [1] 955

Comparing vectors

R provides several methods for comparing vectors, both element-wise and overall.

Element-wise Comparison You can use comparison operators like ==, !=, <, >, <=, and >= to compare elements in two vectors of the same length. The result is a logical vector of the same length, indicating the truth value of each comparison.

# Create two numeric vectors
vector1 <- c(10, 20, 30, 40)
vector2 <- c(15, 20, 25, 40)

# Compare elements using ==
result <- vector1 == vector2
print(result)
## [1] FALSE  TRUE FALSE  TRUE

Selection

Selection on single element

Selects the second elements from each vector, which corresponds to Tuesday’s sales for burger and lemonade, respectively.

# Create a vector of sales_burger
sales_burger <- c(100, 250, 75, 15, 20, 120, 150)
names(sales_burger) <- days_vector
print(sales_burger)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##       100       250        75        15        20       120       150
# Create a vector of sales_lemonade
sales_lemonade <- c(20, 30, 10, 25, 20, 50, 70)
names(sales_lemonade) <- days_vector
print(sales_lemonade)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##        20        30        10        25        20        50        70
# Create a vector of days
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

# Define a new variable based on a selection
# Selection of single element under Tuesday
burger_tuesday <- sales_burger[2]
lemonade_tuesday <- sales_lemonade[2]

# Print result
print(burger_tuesday)
## Tuesday 
##     250
print(lemonade_tuesday)
## Tuesday 
##      30

Selection containing elements

Selects the first, third and fifth elements from both vectors, which correspond to Monday, Wednesday and Friday’s sales for each vector respectively.

# Create a vector of sales_burger
sales_burger <- c(100, 250, 75, 15, 20, 120, 150)
names(sales_burger) <- days_vector

# Create a vector of sales_lemonade
sales_lemonade <- c(20, 30, 10, 25, 20, 50, 70)
names(sales_lemonade) <- days_vector

# Assign variable of days_vector
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

# Define a new variable based on a selection elements (first, mid and last day of the weekdays)
fml_burger <- sales_burger[c(1, 3, 5)]
fml_lemonade <- sales_lemonade[c(1, 3, 5)]

# Print out fml_burger (first, mid and last day of the weekday)
print(fml_burger)
##    Monday Wednesday    Friday 
##       100        75        20
# Print out fml_lemonade (first, mid and last day of the weekday)
print(fml_lemonade)
##    Monday Wednesday    Friday 
##        20        10        20

Selection range elements

Select the first and fourth elements of vector which correspond to Monday to Thursday’s Burget and Lemonade Sales

# Create a vector of sales_burger
sales_burger <- c(100, 250, 75, 15, 20, 120, 150)
names(sales_burger) <- days_vector

# Create a vector of sales_lemonade
sales_lemonade <- c(20, 30, 10, 25, 20, 50, 70)
names(sales_lemonade) <- days_vector

# Assign variable of days_vector
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

# Define a new variable based on a selection from 1st to 4th element
mth_burger <- sales_burger[1:4]
mth_lemonade <- sales_lemonade[1:4]

# Select the 1st and 4th element on mth_burger
print(mth_burger)
##    Monday   Tuesday Wednesday  Thursday 
##       100       250        75        15
# Select the 1st and 4th element on mth_lemonade
print(mth_lemonade)
##    Monday   Tuesday Wednesday  Thursday 
##        20        30        10        25

Selection range with calculation

Calculating Average Weekday Burger and Lemonade Sales

# Create a vector of sales_burger
sales_burger <- c(100, 250, 75, 15, 20, 120, 150)
names(sales_burger) <- days_vector

# Create a vector of sales_lemonade
sales_lemonade <- c(20, 30, 10, 25, 20, 50, 70)
names(sales_lemonade) <- days_vector

# Assign variable of days_vector
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

# Define a new variable based on a selection from Weekdays
weekdays_avg_sales_burger <- sales_burger[1:5]
weekdays_avg_sales_lemonade <- sales_lemonade[1:5]

# Calculate the average sales of burgers from Weekdays
avg_sales_burger <- mean(weekdays_avg_sales_burger)
print(avg_sales_burger)
## [1] 92
# Calculate the average sales of lemonade from Weekdays
avg_sales_lemonade <- mean(weekdays_avg_sales_lemonade)
print(avg_sales_lemonade)
## [1] 21

Selection comparisson

# Create a vector of sales_burger
sales_burger <- c(100, 250, 75, 15, 20, 120, 150)
names(sales_burger) <- days_vector

# Create a vector of sales_lemonade
sales_lemonade <- c(20, 30, 10, 25, 20, 50, 70)
names(sales_lemonade) <- days_vector

# Assign variable of days_vector
days_vector <- c("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")

Burger —-

# Which days did you make a sales more than 50?
sales_burger_above50 <- sales_burger > 50
print(sales_burger_above50)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##      TRUE      TRUE      TRUE     FALSE     FALSE      TRUE      TRUE
# Which days did you make a sales more than 50?
sales_burger_below50 <- sales_burger < 50
print(sales_burger_below50)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##     FALSE     FALSE     FALSE      TRUE      TRUE     FALSE     FALSE
# Prints only the days with sales above 50
print(days_vector[sales_burger_above50])
## [1] "Monday"    "Tuesday"   "Wednesday" "Saturday"  "Sunday"
# Prints only the days with sales below 50
print(days_vector[sales_burger_below50])
## [1] "Thursday" "Friday"

Lemonade —-

# Which days did you make a sales more than 50?
sales_lemonade_above50 <- sales_lemonade > 50
print(sales_lemonade_above50)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##     FALSE     FALSE     FALSE     FALSE     FALSE     FALSE      TRUE
# Which days did you make a sales more than 50?
sales_lemonade_below50 <- sales_lemonade < 50
print(sales_lemonade_below50)
##    Monday   Tuesday Wednesday  Thursday    Friday  Saturday    Sunday 
##      TRUE      TRUE      TRUE      TRUE      TRUE     FALSE     FALSE
# Prints only the days with sales above 50
print(days_vector[sales_lemonade_above50])
## [1] "Sunday"
# Prints only the days with sales below 50
print(days_vector[sales_lemonade_below50])
## [1] "Monday"    "Tuesday"   "Wednesday" "Thursday"  "Friday"