Below are the problems for Homework 1. Please write your solutions inside the provided code blocks.
Note: R will not automatically display results when you assign values to an object.
To show results, either call the object by name or useprint()
.
When finished:
.Rmd
file to Canvas.Perform the following arithmetic operations and include the code and results in your report:
#Your code here
# a. Add two numbers
result_add <- 10 + 5
result_add
## [1] 15
# b. Subtract two numbers
result_sub <- 20 - 7
result_sub
## [1] 13
# c. Multiply two numbers
result_mul <- 8 * 6
result_mul
## [1] 48
#d. Divide two numbers
result_div <- 40 / 8
result_div
## [1] 5
Create two vectors x = c(10, 5, 3.5, 2)
and
y = c(2, 7, 1.5, 4)
and perform the following:
x
and
y
. (5 points)y
from vector x
. (5
points)x
and
y
(element-wise multiplication). (5 points)x
by the corresponding
element of vector y
. (5 points)x
divided by the corresponding element in vector
y
. (10 points)#Your code here
#Create the vectors
x <- c(10, 5, 3.5, 2)
y <- c(2, 7, 1.5, 4)
#display vectors
x
## [1] 10.0 5.0 3.5 2.0
y
## [1] 2.0 7.0 1.5 4.0
# a. add vectors (element-wise)
result_add <- x + y
result_add
## [1] 12 12 5 6
# b. subtract vectors (element-wise)
result_sub <- x - y
result_sub
## [1] 8 -2 2 -2
# c. multiply vectors (element-wise)
result_mul <- x * y
result_mul
## [1] 20.00 35.00 5.25 8.00
# d. divide vectors (element-wise)
result_div <- x / y
result_div
## [1] 5.0000000 0.7142857 2.3333333 0.5000000
# e. element-wise modulo (remainder)
result_mod <- x %% y
result_mod
## [1] 0.0 5.0 0.5 2.0
Perform the following operations in R:
x
with 5 elements of your
choice. (2 points)x
to a threshold value (e.g.,
5) using greater than (>
) and less than
(<
) operators. Store the results in logical vectors. (8
points)L1
and L2
, each
with at least 5 elements, containing a mix of TRUE
,
FALSE
, and NA
values. (5 points)#Your code here
#create a numeric vector x with 5 elements
# a. create a numeric vector
x <- c(2, 7, 5, 10, 3)
x
## [1] 2 7 5 10 3
# b. Compare elements of x to 5
greater_than_5 <- x > 5 # TRUE if element is greater than 5
less_than_5 <- x < 5 # TRUE if element is less than 5
greater_than_5
## [1] FALSE TRUE FALSE TRUE FALSE
less_than_5
## [1] TRUE FALSE FALSE FALSE TRUE
# c. create logical vectors with TRUE, FALSE, and NA
L1 <- c(TRUE, FALSE, NA, TRUE, FALSE)
L2 <- c(FALSE, NA, TRUE, FALSE, TRUE)
# d. Mixed vector: numbers + logical values
mixed_vector <- c(1, 0, TRUE, FALSE, 5)
mixed_vector # Display the vector
## [1] 1 0 1 0 5
class(mixed_vector) #Check its class
## [1] "numeric"
Create two matrices in R and use cbind()
and
rbind()
to combine them.
Instructions:
a. Create a 2 × 3 matrix A
with numbers 1 through 6. (5
points)
b. Create another 2 × 3 matrix B
with numbers 7 through 12.
(5 points)
c. Use cbind(A, B)
to combine the matrices by
columns. (5 points)
d. Use rbind(A, B)
to combine the matrices by
rows. (5 points)
#Your code here
# a. create matrix A (2 rows, 3 columns)
A <- matrix(1:6, nrow = 2, ncol = 3, byrow = TRUE)
A
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
# b. Create matrix B (2 rows, 3 columns)
B <- matrix(7:12, nrow = 2, ncol = 3, byrow = TRUE)
B
## [,1] [,2] [,3]
## [1,] 7 8 9
## [2,] 10 11 12
# c. combine A and B by columns
C_col <- cbind(A, B)
C_col
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 1 2 3 7 8 9
## [2,] 4 5 6 10 11 12
# d. Combine A and B by rows
C_row <- rbind(A, B)
C_row
## [,1] [,2] [,3]
## [1,] 1 2 3
## [2,] 4 5 6
## [3,] 7 8 9
## [4,] 10 11 12
Work with data frames and practice modifying row and column names.
Instructions:
Create a data frame named students
with the
following columns:
Name
= (“Alice”, “Bob”, “Charlie”)Age
= (20, 21, 19)Grade
= (“A”, “B”, “A”)Print the data frame. (5 points)
Change the row names of the data frame to
"S1"
, "S2"
, "S3"
. (5
points)
Change the column names of the data frame to
"StudentName"
, "StudentAge"
,
"StudentGrade"
. (5 points)
#Your code here
# a. Create a data frame with Name, Age, and Grade
students <- data.frame(
Name = c("Alice", "Bob", "Charlie"),
Age = c(20, 21, 19),
Grade = c("A", "B", "A")
)
# b. Display the data frame
students
# c. Change row names to S1, S2, S3
rownames(students) <- c("S1", "S2", "S3")
students
# d. Change column names
colnames(students) <- c("StudentName", "StudentAge", "StudentGrade")
students