0% found this document useful (0 votes)
33 views3 pages

R

R exam

Uploaded by

RaviAnand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views3 pages

R

R exam

Uploaded by

RaviAnand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#Exercise 1

#----------
##1.a. Create a matrix A of integers order (4,5)

##1.b. Create a matrix B of integers order (3,5)

##1.c. Create a matrix C appending matrices A and B row-wise

##1.d. Create a matrix D=ABT

##1.e. Find the inverse of D


### Inverse exists for square matrices. Hence, default solve function throws error.

#Error in solve.default(D) : 'a' (4 x 3) must be square


#Now finding a Moore-Penrose Generalized Inverse of D

##1.f. Find the determinant of D


### D is not a square matrix. Hence, we get an error.

#Error in determinant.matrix(x, logarithm = TRUE, ...) :


#'x' must be a square matrix

##1.g. Write a function which will take the row number r and column number c
and will return the minor of the matrix Mrc

#Exercise 2
#----------
##2.a. Load the datasets mtcars_1.csv and mtcars_2.csv

##2.b. Join the datasets based on the carname

##2.c. Assume the first word of the name represents the company (e.g. Mazda)
and the second word represents the make (e.g. RX4). Create two columns for the
company and make

##2.d. For cars which do not have two words change the name of the make to
'make0'
#The previous command can be corrected to provide 'make0' instead of NA

##2.e. For each of the companies find the average mileage and horsepower

##2.f. Across all cars plot the mileage against the horsepower
##2.g. Plot the displacement vs. mpg split by number of gears and if the
displacement is automatic or manual

carname mpg cyldisp hp drat wt qsec vs am gear carb


Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3

#1
typeof(USPersonalExpenditure)

#2
p <- USPersonalExpenditure[5,]
count<-length(p[])
g<- 1:4
pc<-1:4
count
for(i in 1:count-1){
g[i] <- p[i+1]-p[i]
pc[i] <- (g[i]/p[i])*100
}
g[]
pc[]

#3
mx<-rowSums(USPersonalExpenditure)
mx[]
sum_mx<-sum(mx[])
percent_share<-(mx/sum_mx)*100
max(percent_share)

#4
plot(colnames(USPersonalExpenditure),USPersonalExpenditure[1,1:5],xlab =
'Years',ylab = 'Expenditure')

#5

medi <- c(USPersonalExpenditure[3,1:5])


care <- c(USPersonalExpenditure[4,1:5])
cor(medi,care)

You might also like