# Create a matrix with values 1 to 40
my_matrix <- matrix(1:40, nrow = 8, ncol = 5, byrow = TRUE)
# Print the matrix
print(my_matrix)
## [,1] [,2] [,3] [,4] [,5]
## [1,] 1 2 3 4 5
## [2,] 6 7 8 9 10
## [3,] 11 12 13 14 15
## [4,] 16 17 18 19 20
## [5,] 21 22 23 24 25
## [6,] 26 27 28 29 30
## [7,] 31 32 33 34 35
## [8,] 36 37 38 39 40
# Show the result for column 3
print(my_matrix[, 3])
## [1] 3 8 13 18 23 28 33 38
# Show the result for a row 2
print(my_matrix[2, ])
## [1] 6 7 8 9 10
# Show the result for a row using Modulo operator (Division) where the remainder == 0
print(my_matrix[apply(my_matrix %% 5 == 0, 1, all), ])
## [,1] [,2] [,3] [,4] [,5]
# Show the result using a column & row combination [2 ,5]
print(my_matrix[2, 5])
## [1] 10
# Assign a numeric value to a variable
x <- 42
# Find the data type of the numeric variable
print(class(x))
## [1] "numeric"
# Assign a character value to a variable
y <-("Hello, World!")
# Find the data type of the character variable
print(class(y))
## [1] "character"
values <- c(14, 5, 3, 22, -8, 9, -3, 45, 12, 43, 8, 6)
mean_value <- mean(values)
print(mean_value)
## [1] 13
trimmed_mean <- mean(values, trim = 0.2)
print(trimmed_mean)
## [1] 9.875
median_value <- median(values)
print(median_value)
## [1] 8.5
# Provided values
values <- c(3, 4, 3, 5, 3, 6, 7, 2, 1, 2, 6, 7, 8, 6)
# Mode function
getModes <- function(x) {
uniqx <- unique(x)
tab <- tabulate(match(x, uniqx))
uniqx[tab == max(tab)]
}
# Find the mode(s)
modes <- getModes(values)
print(modes)
## [1] 3 6