I have a matrix that looks like this

```
1 2 8 
2 2 9
3 1 10
4 5 10
5 2 12

```

Now I want to apply some functions on the third columns of the rows that have a certain value in their second column. So for example I only want to consider the rows that have a `2` in their second column. And then I would like to have the mean, the median and the variance of 8,9,12.

In reality my matrix is larger but this is the basic idea. I guess I need a function that first checks the value in the second column and then considers only the values of the third column that correspond with the given value. I made something like this, but it works just for the mean and is actually a waste since that function is already built in R. 

This is what that looked like:

```
average<- function(m){
  k=0
   for (i in 1:nrow(x)){
     if(x[i,2]==m){
       k = k + x[i,3]
     } else {NULL}
     amount <- sum(x[,2]==m)
  }
   av <- k/amount
  return(av)}

```