Thursday, September 7, 2017

max.col() {base}


max.col() function finds the maximum value position for each row in a matrix.

The parameters are:
 - m : matrix 
ties.method: there are three options: “random”, “first”, “last”, the default method is random.

set.seed(123456)
a = round(5 * runif(7))
b = round(6 * runif(7))
c = round(7 * runif(7))
d = round(8 * runif(7))
e = round(9 * runif(7))
datafr = data.frame(a,b,c,d,e)
datafr
##   a b c d e
## 1 4 1 7 1 8
## 2 4 6 6 1 1
## 3 2 1 6 1 5
## 4 2 5 1 4 8
## 5 2 4 2 6 2
## 6 1 5 5 7 1
## 7 3 5 1 7 7
max.col(datafr)
## [1] 5 3 3 5 4 4 4

Using different ties.method:
max.col(datafr, "first")
## [1] 5 2 3 5 4 4 4
max.col(datafr, "random")
## [1] 5 2 3 5 4 4 5
max.col(datafr, "last")
## [1] 5 3 3 5 4 4 5
max.col(datafr[c(1:3)])  #select some columns of the dataframe: from columns 1 to 3
## [1] 3 2 3 2 2 2 2
max.col(datafr[c(1,3,5)])#select some columns of the dataframe: columns 1,3, and 5
## [1] 3 2 2 3 2 2 3

No comments:

Post a Comment

duplicated() {base}

duplicated()  function determines which elements are duplicated and returns a logical vector. The parameters of the function are:   ...