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.
First Let’s create the 3 vectors.
a<-c(2,5,6,7)
b<-c(1,0,9,8)
c<-c(6,5,8,3)
Then we combine the vectors into a matrix and print it.
mat<-matrix(c(a,b,c),byrow = TRUE,nrow = 3)
print(mat)
## [,1] [,2] [,3] [,4]
## [1,] 2 5 6 7
## [2,] 1 0 9 8
## [3,] 6 5 8 3
Now we change the names and print.
colnames(mat)<-c('Mon', 'Tue', 'Wed', 'Thu')
rownames(mat)<-c('Present', 'Absent', 'On leave')
print(mat)
## Mon Tue Wed Thu
## Present 2 5 6 7
## Absent 1 0 9 8
## On leave 6 5 8 3
Finally, we calculate the sums and print.
rsums<-rowSums(mat)
csums<-colSums(mat)
print(rsums)
## Present Absent On leave
## 20 18 22
print(csums)
## Mon Tue Wed Thu
## 9 10 23 18
We can also bind the sums with the matrix
mat<-cbind(mat,rsums)
total<-colSums(mat)
mat<-rbind(mat,total)
print(mat)
## Mon Tue Wed Thu rsums
## Present 2 5 6 7 20
## Absent 1 0 9 8 18
## On leave 6 5 8 3 22
## total 9 10 23 18 60
Read in the dataset mtcars into a dataframe. Plot the following:
First, we read the dataset into a dataframe.
cars<-data.frame(mtcars)
Next we plot the charts
plot(cars$mpg,cars$disp,
xlab = 'MPG',
ylab = 'Disp',
main = 'MPG vs Disp Plot',
col = 'blue')
boxplot(cars$mpg~cars$gear,
xlab = 'Gear',
ylab = 'Distribution of MPG',
main = 'Boxplot of MPG with Gear',
col = 'light blue')
hist(cars$disp,
xlab = 'Disp',
ylab = 'No of Observations',
main = 'Histogram of Disp',
col = 'green')