Professional Documents
Culture Documents
Analytic2 R
Analytic2 R
Analytic2 R
QUESTIONS:
1. Create the following matrices (i) Square Matrix (ii) Identity Matrix (iii)diagonal matrix
2. Using sapply, check that all elements of the list are vectors of the same length. Also calculate the
sum of each
element.
3. We found out that the blood pressure instrument is under-recording each measure and all
measurement
incorrect by 0.1. How would you add 0.1 to all values in the blood vector?
4. We found out that the first patient is 33 years old. How would you change the first element of the
vector
age to 33 years?
equal
to 0. (ii) Replace the third column of A by the sum of the second and third columns(iii) Calculate the
row means
of this matrix (iv) find the transpose of the matrix (v) find the inverse of the matrix
7. You have measured five cylinders, their lengths are: 2.1, 3.4, 2.5, 2.7, 2.9 and the diameters are :
function).(ii) Calculate the volume of each cylinder (V = length * pi * (diameter / .).(iii) Calculate the
mean,
8 Suppose a jar contains r red and b blue marbles and n marbles are selected without replacement.
Write a R
> marbles.probability(8,9,5,2)
ANSWERS:
#question 1
diag(3)
diag(c(1,2,3))
#question 2
sapply(my_list, function(x) {
if (is.vector(x) && length(x) == length(my_list[[1]])) {
sum(x)
} else {
NA
})
#question 3
print(blood)
#question 4
age[1] <- 33
age
#question 5
A = [1 1 3 5 2 6 -2 -1 -3]
A3 = A * [0 0 0; 0 0 0; 0 0 0]
A3
r = A;
#question 6
# Required library for ships dataset
install.packages("MASS")
install.packages("reshape2")
install.packages("reshape")
library(MASS)
library(reshape2)
library(reshape)
# Create dataframe
n <- c(1, 1, 2, 2)
x <- c(6, 3, 2, 5)
y <- c(1, 4, 6, 9)
print(df)
print(molten.data)
# question 7
#corelation
cor(L, D)
#volume
V <- L * pi * (D / 2) ^ 2
#standard deviation
mean(V)
sd(V)
sd(V) / mean(V)
#question 8
marbles.probability(8, 9, 5, 2)