1. Do the following calculations in R
  1. (54)^(45)-56
  2. 23-1*(8-12)
  3. 56/8*(3+4)
  4. 45-5*8+(8+9)

Solution:

# Calculations
resulta <- (5*4)^(4*5)-56
resultb <- 23-1*(8-12)
resultc <- 56/8*(3+4)
resultd <- 45-5*8+(8+9)

# Print results
print(resulta)
## [1] 1.048576e+26
print(resultb)
## [1] 27
print(resultc)
## [1] 49
print(resultd)
## [1] 22

2.2. Create the following vectors a, b, and c a) 2,5,6,7 b) 1,0,9,8 c) 6,5,8,3

Rowbind the vectors to form a 3X4 matrix. Change the column names to (Mon, Tue, Wed, Thu). Change the row names to (Present, Absent, On leave). Calculate the rowsums and the columnsums.

Solution:

# Creating vectors
a <- c(2, 5, 6, 7)
b <- c(1, 0, 9, 8)
c <- c(6, 5, 8, 3)

# Print vectors
print(a)
## [1] 2 5 6 7
print(b)
## [1] 1 0 9 8
print(c)
## [1] 6 5 8 3
# Creating matrix
mat <- rbind(a, b, c)

# Changing row and column names
colnames(mat) <- c("Mon", "Tue", "Wed", "Thu")
rownames(mat) <- c("Present", "Absent", "On leave")

# Print the matrix
print(mat)
##          Mon Tue Wed Thu
## Present    2   5   6   7
## Absent     1   0   9   8
## On leave   6   5   8   3
# Row sums
row_sums <- rowSums(mat)
print(row_sums)
##  Present   Absent On leave 
##       20       18       22
# Column sums
col_sums <- colSums(mat)
print(col_sums)
## Mon Tue Wed Thu 
##   9  10  23  18