R Programming Lab-Assignment 2
R Programming Lab-Assignment 2
BCA-3A and 3E
Write R program to create a 3x3 matrix m1 with random integer values between 1
and 50.
Compute inverse of m1
Create a 3x3 matrix m2 with random integer values between 1 and 50. Write
code to
o add m1 and m2 and store the result in m3, calculate the determinant of m3
o subtract m2 from m1 and store the result in m3, calculate the determinant
of m3
o multiply m1 and m2 and store the result in m3, calculate the determinant
of m3
o element wise multiply m1 and m2 and store the result in m3, calculate the
determinant of m3
Write R program to prove the laws below using A=m1, B=m2, C= m3
o A+B = B+A →Commutative Law of Matrix Addition
o A+B+C = A +(B+C) = (A+B)+C →Associative law of addition
o ABC = A(BC) = (AB)C →Associative law of multiplication
o A(B+C) = AB + AC →Distributive law of matrix algebra
Write R program to prove the rules for transposition of matrices:
(A’)’ = A
(A+B)’ = A’+B’
(AB)’ = B’A’
(ABC) = C’B’A’
1. Loading Data:
o Download the "mtcars" dataset and store it in a variable called df.
2. Filtering Data:
o Write code to select all rows where the car has more than 100
horsepower.
3. Creating Subsets:
o Create a new data frame df_subset that contains only the mpg, cyl,
and hp columns.
4. Sorting Data:
o Sort df_subset in descending order by hp.
1. Creating a Scatterplot:
o Use the ggplot2 library to create a scatterplot of the mtcars dataset, plotting mpg on
the y-axis and hp on the x-axis.
2. Adding a Trend Line:
o Add a trend line to the scatterplot using a linear regression model.
3. Customizing the Plot:
o Customize the plot by adding a title, axis labels, and applying a theme.