Create 3 vectors. Each vector should contain at least 4 data elements. The 3 vectors should contain different types of data. Print each vector, check the type and structure of each vector.
# Creating 3 vectors with different types of data
numeric_vector <- c(1, 2, 3, 4)
character_vector <- c("apple", "banana", "cherry", "date")
logical_vector <- c(TRUE, FALSE, TRUE, FALSE)
# Printing each vector
print(numeric_vector)
## [1] 1 2 3 4
print(character_vector)
## [1] "apple" "banana" "cherry" "date"
print(logical_vector)
## [1] TRUE FALSE TRUE FALSE
# Checking the type and structure of each vector
print(typeof(numeric_vector))
## [1] "double"
print(typeof(character_vector))
## [1] "character"
print(typeof(logical_vector))
## [1] "logical"
print(str(numeric_vector))
## num [1:4] 1 2 3 4
## NULL
print(str(character_vector))
## chr [1:4] "apple" "banana" "cherry" "date"
## NULL
print(str(logical_vector))
## logi [1:4] TRUE FALSE TRUE FALSE
## NULL
3 columns and 3 rows. The two matrices should contain different types of data. Print each matrix. Check the type and structure of each matrix.
# Task 4
# Creating 2 matrices with different types of data
matrix_1 <- matrix(1:20, nrow = 4, ncol = 5)
print("Numeric Matrix:")
## [1] "Numeric Matrix:"
print(matrix_1)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 5 9 13 17
## [2,] 2 6 10 14 18
## [3,] 3 7 11 15 19
## [4,] 4 8 12 16 20
matrix_2 <- matrix(c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"), nrow = 4, ncol = 5)
print("Character Matrix:")
## [1] "Character Matrix:"
print(matrix_2)
## [,1] [,2] [,3] [,4] [,5]
## [1,] "a" "e" "i" "m" "q"
## [2,] "b" "f" "j" "n" "r"
## [3,] "c" "g" "k" "o" "s"
## [4,] "d" "h" "l" "p" "t"
# Checking the type and structure of each matrix
print("Type of Numeric Matrix:")
## [1] "Type of Numeric Matrix:"
print(typeof(matrix_1))
## [1] "integer"
print("Structure of Numeric Matrix:")
## [1] "Structure of Numeric Matrix:"
print(str(matrix_1))
## int [1:4, 1:5] 1 2 3 4 5 6 7 8 9 10 ...
## NULL
print("Type of Character Matrix:")
## [1] "Type of Character Matrix:"
print(typeof(matrix_2))
## [1] "character"
print("Structure of Character Matrix:")
## [1] "Structure of Character Matrix:"
print(str(matrix_2))
## chr [1:4, 1:5] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" ...
## NULL
Create 2 arrays. Each array should contain at least 30 data elements and at least 3 tables. The two arrays should contain different types of data. Print each array. Check the type and structure of each matrix.
# Creating 2 arrays with different types of data
array_1 <- array(1:30, dim = c(2, 5, 3))
array_2 <- array(c("x", "y", "z", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"), dim = c(2, 5, 3))
# Printing each array
print(array_1)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 3 5 7 9
## [2,] 2 4 6 8 10
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 11 13 15 17 19
## [2,] 12 14 16 18 20
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] 21 23 25 27 29
## [2,] 22 24 26 28 30
print(array_2)
## , , 1
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] "x" "z" "b" "d" "f"
## [2,] "y" "a" "c" "e" "g"
##
## , , 2
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] "h" "j" "l" "n" "p"
## [2,] "i" "k" "m" "o" "q"
##
## , , 3
##
## [,1] [,2] [,3] [,4] [,5]
## [1,] "r" "t" "v" "x" "z"
## [2,] "s" "u" "w" "y" "x"
# Checking the type and structure of each array
print(typeof(array_1))
## [1] "integer"
print(typeof(array_2))
## [1] "character"
print(str(array_1))
## int [1:2, 1:5, 1:3] 1 2 3 4 5 6 7 8 9 10 ...
## NULL
print(str(array_2))
## chr [1:2, 1:5, 1:3] "x" "y" "z" "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" ...
## NULL
Create 1 data frame. The data frame should contain 4 columns of data. Columns 1 and 2 should be numeric, column 3 should be logical, column 4 should be character. Print the data frame and check the structure of the data frame.
# Creating a data frame
data_frame <- data.frame(
numeric_column1 = c(1.1, 2.2, 3.3, 4.4),
numeric_column2 = c(5.5, 6.6, 7.7, 8.8),
logical_column = c(TRUE, FALSE, TRUE, FALSE),
character_column = c("alpha", "beta", "gamma", "delta")
)
# Printing the data frame
print(data_frame)
## numeric_column1 numeric_column2 logical_column character_column
## 1 1.1 5.5 TRUE alpha
## 2 2.2 6.6 FALSE beta
## 3 3.3 7.7 TRUE gamma
## 4 4.4 8.8 FALSE delta
# Checking the structure of the data frame
print(str(data_frame))
## 'data.frame': 4 obs. of 4 variables:
## $ numeric_column1 : num 1.1 2.2 3.3 4.4
## $ numeric_column2 : num 5.5 6.6 7.7 8.8
## $ logical_column : logi TRUE FALSE TRUE FALSE
## $ character_column: chr "alpha" "beta" "gamma" "delta"
## NULL
Create 1 list. The list should have at least 4 vectors. Each vector should have at least 5 data elements. The vectors should have different length and contain at least 2 different types of data. Print the list and check the structure of the list.
# Creating a list with vectors of different lengths and data types
mixed_list <- list(
numeric_vector = c(1, 2, 3, 4, 5),
character_vector = c("apple", "banana", "cherry", "date", "fig", "grape"),
logical_vector = c(TRUE, FALSE, TRUE, FALSE, TRUE),
mixed_vector = c(10, "orange", TRUE, "grapefruit", 20)
)
# Printing the list
print("Mixed Data List:")
## [1] "Mixed Data List:"
print(mixed_list)
## $numeric_vector
## [1] 1 2 3 4 5
##
## $character_vector
## [1] "apple" "banana" "cherry" "date" "fig" "grape"
##
## $logical_vector
## [1] TRUE FALSE TRUE FALSE TRUE
##
## $mixed_vector
## [1] "10" "orange" "TRUE" "grapefruit" "20"
# Checking the structure of the list
print("Structure of Mixed Data List:")
## [1] "Structure of Mixed Data List:"
str(mixed_list)
## List of 4
## $ numeric_vector : num [1:5] 1 2 3 4 5
## $ character_vector: chr [1:6] "apple" "banana" "cherry" "date" ...
## $ logical_vector : logi [1:5] TRUE FALSE TRUE FALSE TRUE
## $ mixed_vector : chr [1:5] "10" "orange" "TRUE" "grapefruit" ...
Create another list containing the list you create in task 7. Print the list.
# Creating another list that contains the original list
nested_list <- list(
mixed_list
)
# Printing the nested list
print("Nested List Containing the Original List:")
## [1] "Nested List Containing the Original List:"
print(nested_list)
## [[1]]
## [[1]]$numeric_vector
## [1] 1 2 3 4 5
##
## [[1]]$character_vector
## [1] "apple" "banana" "cherry" "date" "fig" "grape"
##
## [[1]]$logical_vector
## [1] TRUE FALSE TRUE FALSE TRUE
##
## [[1]]$mixed_vector
## [1] "10" "orange" "TRUE" "grapefruit" "20"
# Access the first vector in the nested_list
print("Access the first vector in the nested_list:")
## [1] "Access the first vector in the nested_list:"
nested_list[[1]]$numeric_vector
## [1] 1 2 3 4 5