# R – Exercise III

1) Create a matrix named m1 with three rows and five columns and all the numeric (integer) values from 6 to 20!

```m1 <- matrix(6:20, nrow = 3, ncol = 5)

m1
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    6    9   12   15   18
## [2,]    7   10   13   16   19
## [3,]    8   11   14   17   20
```

2) Multiply all elements in m1 by 0.5! Overwrite the matrix m1 with the result!

```m1 <- m1 * 0.5
```

3) Create another matrix m2 with one row and five columns and all the numeric (integer) values from 1 to 5!

```m2 <- matrix(1:5, nrow = 1, ncol = 5)

m2
##      [,1] [,2] [,3] [,4] [,5]
## [1,]    1    2    3    4    5
```

4) Calculate the sum of all elements in m2!

```sum(m2)
## [1] 15
```

5) Combine m1 and m2 with rbing(). Save the result as m3 and check the dimension of the new matrix!

```m3 <- rbind(m1, m2)

m3
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  3.0  4.5  6.0  7.5  9.0
## [2,]  3.5  5.0  6.5  8.0  9.5
## [3,]  4.0  5.5  7.0  8.5 10.0
## [4,]  1.0  2.0  3.0  4.0  5.0

dim(m3)
## [1] 4 5
```

6) Index the 5th column of m3!

```m3[ , 5]
## [1]  9.0  9.5 10.0  5.0
```

7) Index the 2nd and 4th lines of m3!

```m3[ c(2, 4), ]
##      [,1] [,2] [,3] [,4] [,5]
## [1,]  3.5    5  6.5    8  9.5
## [2,]  1.0    2  3.0    4  5.0
```

8) Calculate the sums for all columns in m3!

```colSums(m3)
## [1] 11.5 17.0 22.5 28.0 33.5
```

9) Calculate the standard deviation for the 3rd column in m3!

```sd( m3[ , 3] )
## [1] 1.796988
```

10) From m3, index the element in the 2nd column and 2nd line and all eight adjacent elements! Save the result as m4 and examine its object class!

```m4 <- m3[2:4, 2:4]

m4
##      [,1] [,2] [,3]
## [1,]  5.0  6.5  8.0
## [2,]  5.5  7.0  8.5
## [3,]  2.0  3.0  4.0

class(m4)
## [1] "matrix"
```